Search code examples
phpstring-matchingstrpos

strpos - user agent partial matching


I am trying to detect 2 different phones based on their user agents.

I used strpos but my code is outputting "other phone" on both phones.

$ua = $_SERVER['HTTP_USER_AGENT']; 


$phone1 = "SM-G900F";
$phone2 = "GT-I9505";


if(strpos($phone1,$ua)!==false){
 echo "gs 5";
 }

elseif (strpos($phone2,$ua)!==false){
 echo "gs 4";
} else {
  echo "other phone";
}

Solution

  • Your arguments are backwards, the syntax is

    strpos($haystack, $needle)
    

    You're basically saying is 'Argle bargle Foo SM-G900F Bar Baz' contained in the string 'SM-G900F', for which the answer is obviously "NO".

    Try

    if(strpos($ua, $phone1)!==false){
    

    instead.