Originally posted by sidney
whoops coding after comiming in from pub
return (bool)strpos(' '.stripslashes($haystack),stripslashes(
$needle,1)
);
yeah, but now what happens if all or part of the needle is at position 0? now you cant even find it because your skipping the first character.
you MUST use the === or !== operators w/ strpos if your just checking if the needle exists or not. there is no way around that, you just simply need to use it, thats why those operators exist, because without them many things wouldnt be possible.
and while were all sharing our insignificantly different methods of using strpos, heres mine ๐
return (false !== strpos($haystack, $needle));
also strstr is not really a good way to do it either. you still need to explicty check for false using === or !==
take this example
$needle = '0';
$haystack = '1110';
if (strstr($haystack, $needle)) {
echo "$needle is in $haystack";
} else {
echo "$needle is NOT in $haystack";
}
many would expect this to work, but it wont. strstr will return everything from the first occurance of $needle to the end of $haystack
so in this case, strstr will return string 0
string 0 evaluates to boolean false.
hence why you must do this
$needle = '0';
$haystack = '1110';
if (false !== strstr($haystack, $needle)) {
echo "$needle is in $haystack";
} else {
echo "$needle is NOT in $haystack";
}