How to return position of separate word for example:
echo strpos("I love academyphp, I love php too!","php");
I want to find only position of free words php and ignore position of academyphp.
Add space to "php" and add 1:
echo strpos("I love academyphp, I love php too!"," php")+1;
Or two spaces, if you want it to be "free" on both sides:
echo strpos("I love academyphp, I love php too!"," php ")+1;
You'll need a more versatile function. \b
is a word boundary:
preg_match('/\bphp\b/', $string, $matches, PREG_OFFSET_CAPTURE);
echo $matches[0][0] . ' found at position ' . $matches[0][1];
If you need to find more than one use preg_match_all
.