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)spamthis 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

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -