Let's say you have string $haystack = "mother" and string $needle = "other".
You want to find out if $haystack contains $needle.
One option is to use the strpos PHP function.
$pos = strpos($haystack,$needle);
if($pos === false) {
// string needle NOT found in haystack
}
else {
// string needle found in haystack
}
!! Watch out, this can be tricky. Some people do just something like
if ($pos > 0) {} or perhaps something like
if (strpos($haystack,$needle)) { } and forget that your string can be right at the beginning.
if (strpos($haystack,$needle)) { } does not produce the same result as our code shown above.
Another option is to use strstr
if (strlen(strstr($haystack,$needle))>0) { echo "Needle Found"; }
Strstr function is case-sensitive. For a case-insensitive search, use stristr().
The strpos() function is faster and less memory intensive.