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

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -