I have a $sentence i look through and a $word I'm trying to match. The $word can be anything, but it's not an expression so i escape regex operators: $word = addcslashes($word,".[]()^$/*+|"); Everything works fine untill now and the following example returns what's expected:
Code: Select all
$word="[xyz]*";
$sentence1="[xyz]*";
$sentence2="xyz";
$word = addcslashes($word,".[]()^$/*+|"); // $word becomes "/[xyz/]/*"
preg_match("/$word/i", $sentence1); //returns true
preg_match("/$word/i", $sentence2); //returns false
Code: Select all
$word="[xyz]*";
$sentence1="[xyz]*";
$sentence2="xyz";
$word = addcslashes($word,".[]()^$/*+|"); // $word becomes "/[xyz/]/*"
preg_match("/\b$word\b/i", $sentence1); //returns false <--problem!!
preg_match("/\b$word\b/i", $sentence2); //returns false