0

只是一个关于性能和可扩展性的简单问题。我需要从其用户代理字符串中识别出 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循环?

非常感谢你。

4

1 回答 1

1

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

$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;
于 2012-05-10T10:04:01.243 回答