phpregexvalidationcpu-wordprofanity

Validate the existence of banned words including near spellings in a string


I can't get the preg_match to find a word anywhere in a 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


Solution

  • \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