i’m triyin to match some phrase inside a string as a word (stristr doesn’t work beacause i dont want results that contain the “word”)
I use this code:
function striword($string, $word) {
return preg_match("/(?:[[:space:]]|^)" . $word . "(?:[^\w]|$)/i", $string);
}
But when I try to match a string like “this is a string” it doesn’t work as expected 🙁
Example:
//Phrase to match: "soda and beer"
striword($string, "soda and beer");
String 1: "I like soda and beer" MATCH: TRUE
String 2: "I like soda and beerbum" MATCH: FALSE
String 3: "I like soda and beer, it's nice!" MATCH: TRUE
Two options:
Either use word boundary anchors (useful if you’re dealing with actual, alphanumeric words):
Or use spaces as separators:
If you’re using Unicode strings and want to match Unicode words, don’t forget the
/umodifier.