Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
搜索字符串并用php检查结果是否在列表中_Php - Fatal编程技术网

搜索字符串并用php检查结果是否在列表中

搜索字符串并用php检查结果是否在列表中,php,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-T95

只是一个关于性能和可伸缩性的简单问题。 我需要从它的用户代理字符串中识别出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;

for循环和数组意味着在整个数组中循环,直到找到匹配项。你目前采取的方法很好。谢谢gunnx,很好。您是否认为此解决方案比我的解决方案速度快,服务器请求量大?我建议对此进行测试,但我认为(如果没有,有人会纠正我)strps比strstr快。我不知道谁认为此响应没有用处(将其标记为负值),但了解原因将有助于并符合这个社区的精神!谢谢@gunnx,但是如何使用数组和foreach循环来提取其值呢?根据您的经验,此解决方案是否比if周期更快?不管怎样,我已经接受了你的答案,如果你愿意,请投票支持我的问题;)谢谢。你有没有办法对网页上的HTTP请求进行基准测试,这样你就可以进行测试?