According to this \b
does not support anything other than [A-Za-z0-9_]
.
Note that you have to escape your Regex, as you're generating it from a string (and the PHP compiler, at the time it creates this string, doesn't know it's a Regex).
Using the Regex /(^|\s)WORD($|\s)/i
seems to work.
Code example:
$text = "This is a $1ut ( Y ) @ss @sshole a$$ ass test with grass and passages.";
$blacklist = array(
'$1ut',
'( Y )',
'@ss',
'@sshole',
'a$$',
'ass'
);
foreach ($blacklist as $word) {
$pattern = "/(^|\\s)" . preg_quote($word) . "($|\\s)/i";
$replace = " " . str_repeat('*', strlen($word)) . " ";
$text = preg_replace($pattern, $replace, $text);
}
echo $text;
Output:
This is a **** ***** *** ******* *** *** test with grass and passages.
Be aware that if your string starts or ends with one of these words, we'll add a space to the match in each end, meaning that there'll be a space before or after the text. You can take care of this with trim()
Update;
Also be aware that this doesn't account for punctuation in any way.
the other user has an ass. and it is nice
would go through for example.
To conquer this, you could extend it even further:
/(^|\\s|!|,|\.|;|:|\-|_|\?)WORD($|\\s|!|,|\.|;|:|\-|_|\?)/i
This would mean that you also had to change the way we're replacing:
$text = "This is a $1ut ( Y ) @ss?@sshole you're an ass. a$$ ass test with grass and passages.";
$blacklist = array(
'$1ut',
'( Y )',
'@ss',
'@sshole',
'a$$',
'ass'
);
foreach ($blacklist as $word) {
$pattern = "/(^|\\s|!|,|\\.|;|:|\\-|_|\\?)" . preg_quote($word) . "($|\\s|!|,|\\.|;|:|\\-|_|\\?)/i";
$replace = '$1' . str_repeat('*', strlen($word)) . '$2';
$text = preg_replace($pattern, $replace, $text);
}
echo $text;
and add all the other punctuation etc.
Output:
This is a **** ***** ***?******* you're an ***. *** *** test with grass and passages.