regex - Matching word with php -
after doing research can't seem find solution problem. have list of bad words , want able see if user left comment of words. have tried different regular expressions no success. btw im no regex guru.
lets have word $word = 'bi'
on list. , comment says: $comment = bi
, using preg_match($pattern, $comment)
parent has been: 1)$word#i
2)/\s+($word)/\s+/i
3)/\b($word)/\b/i
with code:
if (preg_match($pattern, $commentdata['comment_content'])) { echo 'spam'; } else { echo 'true' }
i get:
1)spam
this case words linke combination
dont want block
2)true
3)true
how can make pattern matches word , not word within?
this job, near solution:
preg_match("~\b$word\b~i", $comment);
for particular cases 'bi-directional' :
you can use instead:
preg_match("~(?<![a-z]-)\b$word\b(?!-[a-z])~i", $comment);
Comments
Post a Comment