Hello people :) I'm trying to use strpos() to find whole words in a sentence. But at the moment, it also find the word if it just are a part of another word. For example:
$mystring = "It's a beautiful morning today!";
$findme = "a";
$pos = strpos($mystring, $findme);
In this example it would find "a" in the word "a" (as it should do), but also in "beautiful" because there is an a in it.
How can i find only whole words and not if it is a part of other words?
Use regex , with the word boundary delimiter
\b
, like this :returns
regex is hard for beginners, another way and I admit it isn't the best, would be to use
$findme = " a "
This is a job for regex:
Basically, it's finding the letters surrounded by a word-boundary (the
\b
bits). So, to find the position: