tags:

views:

42

answers:

1

I can't get the preg_match to find a word anywhere a the string.

I have this:

$bad_words = "/(\bsuck\b)|(\bsucks\b)|(\bporn\b)|";
$text = "sucky";

if(preg_match($bad_term_filter, trim($feedback_review_comment)) != 0 )

I need to return true but it only returns true if its an exact match, for example if

 $text = "suck";

that returns true

+1  A: 

\b is the word boundary anchor. It looks like you're trying to find if some word occurs anywhere regardless of the word boundaries, so I think the pattern you want is simply:

suck|porn

You also do not want the last empty alternate, because that will match everything (all string contains an empty string). There is no need to explicitly look for sucks, because it already contains suck.

References

polygenelubricants
that doesnt fix the issue \b(sucks?|porn)\bbecause suckiiiiiii gets by
Matt
@Matt: what about `xsuckx`? Do you want to match that too? If so, just get rid of all `\b` because apparently you don't need it.
polygenelubricants