简体   繁体   中英

Replace words using regular expression

I have a website and user content needs to be filtered before posted (comments etc). At the moment I have a system the scans posted content against a word list and then replaces these words with asterisks.

This works fine for single words, but I am now looking to replace a word sequence and I am kind of lost.

For the example we'll use PayPal. At the moment my regular expression finds and replaces this fine, however if I wanted to search and replace 'Pay Pal' it doesn't. Here is my replacement code that works for single words so far:

$word = $words->word;
$length = strlen($word);

$replacement = str_repeat('*', $length);

$newContent = preg_replace('/\b'.$word.'\b/i', $replacement, $content);

So I would need it to replace 'pay pal' with ' * * '.

Ideally as well the space would be a wildcard to pick up things such as 'pay_pal', but this is just a nice to have.

I've played about but to no avail.

To clarify - how do I modify this to replace two words as well as one?

$newContent = preg_replace('/\b'.$word.'\b/i', $replacement, $content);

That's bad. Really, really bad. It's like taking a helicopter to go shopping 20 meters away from home.

For entirely solid chunks of text use str_replace() .

$newContent = str_replace($word, $replacement, $content);
// If you want it to be surrounded with spaces use the one below:
$newContent = str_replace(" $word ", $replacement, $content);

For more complex like "PayPal", I suggest you storing it in form of "Pay*Pal", or other way. Example:

$badWord = 'Pay*Pal';
$pattern = '~\b'.str_replace('*','.?',$badWord).'\b~Ui'; 
// dont use 'i' flag if you want it case-sensitive
$newContent = preg_replace($pattern, $replacement, $content);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM