Search code examples
phpstrpos

strpos function not working with special character


I want to check if string does NOT contain word "★ StatTrak™". But it is not working properly when I include ★ character. Without "★" and "™" it's working fine.

I tried using mb_strpos and urldecode before checking.

$name_to_check = '★ StatTrak™';

if(!strpos($name_to_check,'★ StatTrak™') !== false){
   echo 'not contain';
}else{
   echo 'contain';
}

Solution

  • is a multi byte character and strpos cannot understand this.

    What you are looking for is mb_strpos. This function performs the same action, but is multi-byte characters safe.


    Update: While the answer above is correct, it was not the entire solution to your case. The problem is you are making the if check too complicated.

    You are first inverting the result of strpos and then checking if it does not match false. I don't think this was your intention. To fix this, just check if the result of mb_strpos equals false.

    if (mb_strpos($name_to_check, '★ StatTrak™') === false)
        echo 'not contain';
    } else {
        echo 'contain';
    }