Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make strpos case insensitive

Tags:

php

strpos

People also ask

Is Strpos case-insensitive?

The stripos() function finds the position of the first occurrence of a string inside another string. Note: The stripos() function is case-insensitive.

Is str contains case-sensitive PHP?

4 Answers. Show activity on this post. Note that strpos() is case sensitive, if you want a case-insensitive search, use stripos() instead.

What does case-insensitive mean?

case insensitive (not comparable) (computer science) Treating or interpreting upper- and lowercase letters as being the same.

What is the strpos () function used for?

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 />;
}