php - Search article for exact match keywords? Standard regex I found doesn't work -
i'm working wordpress articles, searching keywords "sex" , not "sexually".
i've tried
$string = "testing placing word @ end sex. more words here."; if ( preg_match("/\sex\b/i", strtolower($string) ) ) {}
and
$string = "testing placing word @ end sex. more words here."; if ( preg_match("~\sex\b~", strtolower($string) ) ) {}
but both return false.
obviously in article, word can @ beginning, end, near punctuation, etc. how can account this?
you're missing character - notice \b
after sex? means it's word boundary. need 1 before sex
well, not \s
- means whitespace.
$string = "testing placing word @ end sex. more words here."; if ( preg_match("~\bsex\b~", strtolower($string) ) ) {} ^---- b here
Comments
Post a Comment