0

After doing some research I can't seem to find a solution to my problem. I have a list of bad words and I want to be able to see if a user left a comment with any of those words. I have tried different regular expressions with no success. BTW Im no regex guru.

Lets say I have the word $word = 'bi' on my list. And a comment that says: $comment = he is bi, using preg_match($pattern, $comment) where parent has been: 1)$word#i

2)/\s+($word)/\s+/i

3)/\b($word)/\b/i

With this code:

if (preg_match($pattern, $commentdata['comment_content'])) {
    echo 'spam';
 }
 else {
     echo 'true'
 }

I get:

1)spamthis is also the case for words linke combination which I dont want to block

2)true

3)true

How can I make a pattern that only matches the word and not the word within?

4

2 回答 2

1

这完成了工作,你接近解决方案:

preg_match("~\b$word\b~i", $comment);

对于某些特殊情况,例如“双向”:

您可以改用:

preg_match("~(?<![a-z]-)\b$word\b(?!-[a-z])~i", $comment);
于 2013-05-09T20:00:34.193 回答
1
$pattern = "/\b{$word}/\b/i" ;

或者

$pattern = "/(\b{$word}\b)/i" ;

会做这项工作。

于 2013-05-09T20:05:27.983 回答