A recent problem I've encountered
$string = 'Demetria Devonne';
if (strpos($string, 'Devon'))
{
echo 'Devon';
}
Returns a result. But I need to only find matches for the specified word where a letter does not come before or after.
So running a check on words like
"Devons" or "Devonia"
would not result in a match.
But then something like
"From Devon, Uk"
would result in a match as there are no letters surrounding it.
Is there a function that can do this in PHP?
You will need to do this using regex, rather than string functions. Fortunately, this is very simple in this case.
if (preg_match('/\bDevon\b/', $string)) {
echo 'Devon';
}
\b
is a special character that means "word boundary", such as a comma or a space.
0 comments:
Post a Comment