How to make strpos case insensitive

前端 未结 4 759
离开以前
离开以前 2020-11-28 10:47

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 wil

相关标签:
4条回答
  • 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 />;
    }
    
    0 讨论(0)
  • 2020-11-28 11:22

    http://www.php.net/manual/en/function.stripos.php

    stripos() is not case-sensitive.

    0 讨论(0)
  • 2020-11-28 11:28

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

    0 讨论(0)
  • 2020-11-28 11:34

    'i' in stripos() means case insensitive

    if(stripos($product->name, $searchterm) !== false){ //'i' case insensitive
            echo "Match = ".$product->link."<br />;
        }
    
    0 讨论(0)
提交回复
热议问题