How to make strpos case insensitive

EnexoOnoma picture EnexoOnoma · Jul 22, 2011 · Viewed 76.8k times · Source

How can I change the strpos to make it non case sensitive. The reason is if the product->name is MadBike and the search term is bike it will not echo me the link. My main concern is the speed of the code.

<?php
$xml  = simplexml_load_file('test.xml');
$searchterm = "bike";
foreach ($xml->product as $product) {
if (strpos($product->name, $searchterm) !== false ) {
echo $product->link;
} }
?>

Answer

Dereleased picture Dereleased · Jul 22, 2011

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).