regex - How to find a whole word in a string in PHP without accidental matches? -
parsing array of string commands, need know if string contains specific keyword.
sounds simple know, problem comes when command keyword may part of word.
ex:
checksound sound check so need check if current line has checksound, sound, or check command.
if use like:
if(stristr($line,'sound') == true) then may find checksound before sound , not parse correctly.
question:
is there way find occurrence of whole word sound , ignore occurrence sound if found part of word checksound?
i sure missing simple here.
you can use regular expression achive goal.
example #2 documentation of preg_match:
/* \b in pattern indicates word boundary, distinct * word "web" matched, , not word partial "webbing" or "cobweb" */ if (preg_match("/\bweb\b/i", "php web scripting language of choice.")) { echo "a match found."; } else { echo "a match not found."; } note above example uses i modifier, makes search "web" case insensitive.
Comments
Post a Comment