renevdkooi
renevdkooi

Reputation: 1643

PHP: regex replace weird result

I have a list of badwords one of them is "S.A." All my words are saved in an array and I loop through the array and do the replacement. The word S.A. replaces things which I don't want replaced, i need it only to replace S.A. as a word itself. So example "This S.A. was bad." should become "This was bad". But now when I run it on a string with (for example) SEAS in it, it will replace it... no idea why...

ps: the if then check is because if the string $v is exactly the badword, it should not be removed. Shouldn't the \b word indicator in the regex make the word an exact match?

ps: I only want to remove full words, not a part of a word. If I have Apple in the badlist but someone wrote Apples it should not be replaced.

foreach ($this->badwordArr as $badword) {
    if (strtoupper($v) == strtoupper($badword)) {

        // no replace because its the only word
        $data[$key] = $v;

    } else {

        $pattern = "/\b$badword\b/i";
        $v = preg_replace($pattern, " ", $v);
    }       
}

What's wrong with my regex pattern?!?!

Upvotes: 1

Views: 74

Answers (1)

jeroen
jeroen

Reputation: 91734

The dot is a meta character in a regex that represents any character. You need to escape it:

S\.A\.

To avoid revising your whole list, you can use:

$badword_escaped = preg_quote($badword);
$pattern = "/\b$badword_escaped\b/i";

Upvotes: 5

Related Questions