2012-05-10 20 views
0

只是关于性能和可伸缩性的一个简单问题。 我需要从其用户代理字符串中识别Android手机的确切型号,然后在模型位于特定列表中时调用页面。所以我用“stristr”功能和简单的if条件,以下列方式:搜索字符串并检查结果是否在使用php的列表中

$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); 
    } 

所以现在的问题是:是否有一个更优雅,也许更好(快)的方式,使这种比较?我想用一个数组和一个循环?

非常感谢你提前。

+2

一个for循环和数组是指通过整个数组循环,直到找到一个匹配。你目前采取的方法很好。 – 2012-05-10 09:48:13

回答

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; 
+0

感谢gunnx,很好。你认为这个解决方案比我的服务器请求数量大吗? – bobighorus

+0

我会建议在这方面运行测试,但我认为(有人会纠正我,如果不是)strpos比strstr更快 – gunnx

+0

我不知道谁认为这个响应没有用(标记为负值),但它会有帮助,并符合这个社区的精神,知道为什么! – bobighorus

相关问题