J
Jason C
I'm building a profanity filter, and I'm using the following subroutine to replace matched words with XXXX:
while (($original, $converted) = @profanityArr) {
if (!$converted) {
$len = length($original);
$converted = "X" x $len;
}
$original = quotemeta($original);
$text =~ s/(\r|\n|\r\n|<br>|\s)*$original(\r|\n|\r\n|<br>|\s)*/$1$converted$2/i;
}
# When I feed:
$original = "daym";
$text = "<br><br>daym<br><br>";
###
I'm getting "<br>XXXX<br>". Meaning, it loses the matched <br> in both $1 and $2.
# When I feed:
$original = "jason";
$converted = "brainfried";
$text = "<br><br>jason<br><br>";
###
I'm getting "<br>brainfried<br>". Again, it loses the matched <br> in both $1 and $2.
# When I feed:
$original = "dammit";
$converted = "XXXXit";
$text = "<br><br>dammit<br><br>";
###
I'm getting "<br>XXXXit<br><br>". Meaning, it loses the matched <br> in $1, but keeps it in $2.
It's the same if I change $1 and $2 to \1 and \2.
Any suggestions on how to correct the sub to keep the matched <br>?
while (($original, $converted) = @profanityArr) {
if (!$converted) {
$len = length($original);
$converted = "X" x $len;
}
$original = quotemeta($original);
$text =~ s/(\r|\n|\r\n|<br>|\s)*$original(\r|\n|\r\n|<br>|\s)*/$1$converted$2/i;
}
# When I feed:
$original = "daym";
$text = "<br><br>daym<br><br>";
###
I'm getting "<br>XXXX<br>". Meaning, it loses the matched <br> in both $1 and $2.
# When I feed:
$original = "jason";
$converted = "brainfried";
$text = "<br><br>jason<br><br>";
###
I'm getting "<br>brainfried<br>". Again, it loses the matched <br> in both $1 and $2.
# When I feed:
$original = "dammit";
$converted = "XXXXit";
$text = "<br><br>dammit<br><br>";
###
I'm getting "<br>XXXXit<br><br>". Meaning, it loses the matched <br> in $1, but keeps it in $2.
It's the same if I change $1 and $2 to \1 and \2.
Any suggestions on how to correct the sub to keep the matched <br>?