开发者

Regular expression problems

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";
开发者_Go百科

that returns true


\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

  • regular-expressions.info/Anchors and Character Classes, and Optional
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜