I’ve a code,
$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 = "/\b". preg_quote($word) ."\b/i";
$replace = str_repeat('*', strlen($word));
$text = preg_replace($pattern, $replace, $text);
}
print_r($text);
which return the following result:
This is a $1ut ( Y ) @ss @sshole a$$ *** test with grass and passages.
When I remove word boundary from regexp,
$pattern = "/". preg_quote($word) ."/i";
it return:
This is a **** ***** *** ***hole *** *** test with gr*** and p***ages.
How can I write the regexp so it wouldn’t replace such words as passages, grass etc. but completely replace such as @sshole?
According to this
\bdoes 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)/iseems to work.Code example:
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 nicewould go through for example.To conquer this, you could extend it even further:
/(^|\\s|!|,|\.|;|:|\-|_|\?)WORD($|\\s|!|,|\.|;|:|\-|_|\?)/iThis would mean that you also had to change the way we’re replacing:
and add all the other punctuation etc.
Output:
This is a **** ***** ***?******* you're an ***. *** *** test with grass and passages.