Hello everyone. I'm having an issue comparing strings:
I am trying to find a case insensitive PHP function that checks if a string is a part of another string. For instance if you compare "Hello my Name is Teo" with "my nAMe" it would return true or 1. Is there something like this? I have a huge list of string functions but I can't find something that works...
if(stripos($haystack, $needle) !== false)
{
// it's in there
}
Note the use of the !== false comparison, as it will return integer 0 if found at the start of the string, which would be treated as a false with a non-type-checking == or != operator.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Kindly listen to the advice posted by others before claiming resolution.
From PHP.net:
substr_count() returns the number of times the needle substring occurs in the haystack string. Please note that needle is case sensitive.
Your original question was quite clear in stating you wanted a case insensitive comparison, you even put that in upper case to emphasize.
stripos(), available in PHP5, is case insensitive, strpos is not. Also, just so you know, the function you used doesn't count overlapped substrings and stripos is also binary safe, which can be handy in some situations.
Sorry, but had to post this, no disrespect intended.
Bookmarks