The stripos() function finds the position of the first occurrence of a string inside another string. Note: The stripos() function is case-insensitive.
4 Answers. Show activity on this post. Note that strpos() is case sensitive, if you want a case-insensitive search, use stripos() instead.
case insensitive (not comparable) (computer science) Treating or interpreting upper- and lowercase letters as being the same.
strpos in PHP is a built-in function. Its use is to find the first occurrence of a substring in a string or a string inside another string. The function returns an integer value which is the index of the first occurrence of the string.
You're looking for stripos()
If that isn't available to you, then just call strtolower()
on both strings first.
EDIT:
stripos()
won't work if you want to find a substring with diacritical sign.
For example:
stripos("Leży Jerzy na wieży i nie wierzy, że na wieży leży dużo JEŻY","jeży");
returns false
, but it should return int(68)
.
http://www.php.net/manual/en/function.stripos.php
stripos() is not case-sensitive.
'i' in stripos() means case insensitive
if(stripos($product->name, $searchterm) !== false){ //'i' case insensitive
echo "Match = ".$product->link."<br />;
}
make both name & $searchterm lowercase prior to $strpos.
$haystack = strtolower($product->name);
$needle = strtolower($searchterm);
if(strpos($haystack, $needle) !== false){
echo "Match = ".$product->link."<br />;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With