matching a word to end of string with strpos
strpos turned out to be the most efficient. Can be done with
substr but that creates a temporary substring. Can also be done with regex, but slower than strpos and does not always produce the right answer if the word contains meta-characters (see Ayman Hourieh comment).
if(strlen($str) - strlen($key) == strrpos($str,$key)) print "$str ends in $key"; // prints Oh, hi O ends in O
and best to test for strict equality
=== (see David answer)
Thanks to all for helping out.
I’m trying to match a word in a string to see if it occurs at the end of that string. The usual
strpos($theString, $theWord); wouldn’t do that.
$theWord = "my word";
$theString = "hello myword"; //match $theString = "myword hello"; //not match $theString = "hey myword hello"; //not match
What would be the most efficient way to do it?
P.S. In the title I said
strpos, but if a better way exists, that’s ok too.