搜索字符串并检查结果是否在带有 PHP 的列表中


search into a string and check if the result is in a list with php

只是一个关于性能和可扩展性的简单问题。我需要从其用户代理字符串中识别Android手机的确切型号,然后在型号在特定列表中时调用页面。所以我使用"stristr"函数和一个简单的if条件,方式如下:

$ua = $_SERVER['HTTP_USER_AGENT'];
if ( stristr($ua, "Nexus S") || stristr($ua, "GT-I9003")  || stristr($ua, "GT-I9000") || stristr($ua, "SGH-T959D") || stristr($ua, "SGH-I897") || stristr($ua, "GT-I9088") || stristr($ua, "GT-I9100")  ) {
        $page = "android_specific.html";
        header('Location: ' . $page);
    } 

所以问题是:有没有一种更优雅、也许更好(更快)的方式来进行比较?我猜有一个数组和一个 for 循环?

提前非常感谢你。

使用数组可能会使更新更简单

$ua = "User agent is Nexus S";
$agents = array("Nexus S","GT-I9003");
$page = "default.html";
foreach ($agents as $agent)
{
  if (stripos($ua,$agent)!==FALSE)
  {
    $page = "andriod.html";
    break;
  }
}
echo $page;