在 PHP 中使用 strpos() 匹配的第一个字符串处停止
这可能是一些简单愚蠢的事情,但我自己无法弄清楚。
我正在尝试制作一个移动检测脚本并取得了巨大的成功。但经过更深入的检查,我发现我的一个 IF
子句似乎返回 TRUE
但匹配错误。
我有这个数组:
private $arrAgent = array(
'sony',
'symbian',
'nokia',
'samsung',
'mobile',
'windows ce',
'blackberry',
'ericsson',
'danger',
'palm',
'series60',
'palmsource',
'pocketpc',
'smartphone',
'vodafone',
'iphone',
'ipad',
'android'
);
然后我有一个函数可以循环该数组并查找它是否匹配
private function detectMobileAgent() {
if ($this->MobileDevice === false) {
foreach ($this->arrAgent as $key => $value) {
if (strpos(Server::userAgent(), $value) !== false) {
$this->MobileDevice = true;
// echo $value;
break;
}
}
}
}
现在的问题是我在 iPad/iPhone userAgent 中发现了一个错误,它阻止了我清晰的读取。
iPad 用户代理看起来像这样:
mozilla/5.0 (ipad; u; cpu os 4_3_2 like mac os x; en-us) applewebkit/533.17.9 (khtml, like gecko) version/5.0.2 mobile/8h7 safari/6533.18.5
我从 xCode 附带的 iOS 模拟器中得到了它,在我的实际 iPad 上我看到了几乎相同的用户代理,只是操作系统版本和 safari 版本不同。
现在我的问题是,在该用户代理中,字符串位置返回 ipad
和 mobile
的匹配项,如何让它在第一个字符串匹配后停止?
This maybe some simple silly thing but I can't figure it out on my own.
I am trying to make a mobile detection script and have had great success. But upon deeper inspection I found that one of my IF
clauses seems to be returning TRUE
but with the wrong match.
I have this array:
private $arrAgent = array(
'sony',
'symbian',
'nokia',
'samsung',
'mobile',
'windows ce',
'blackberry',
'ericsson',
'danger',
'palm',
'series60',
'palmsource',
'pocketpc',
'smartphone',
'vodafone',
'iphone',
'ipad',
'android'
);
then I have a function that will loop the array and find if it matches
private function detectMobileAgent() {
if ($this->MobileDevice === false) {
foreach ($this->arrAgent as $key => $value) {
if (strpos(Server::userAgent(), $value) !== false) {
$this->MobileDevice = true;
// echo $value;
break;
}
}
}
}
Now the problem is that I found an error in the iPad/iPhone userAgent that prevents from me getting a clear reading.
The iPad user agent looks like this:
mozilla/5.0 (ipad; u; cpu os 4_3_2 like mac os x; en-us) applewebkit/533.17.9 (khtml, like gecko) version/5.0.2 mobile/8h7 safari/6533.18.5
I got that from a iOS simulator that comes with xCode, on my actual iPad I saw an almost identical user agent, just the OS version and safari version were different.
Now my problem is that in that user agent, the string position returns a match for ipad
AND mobile
, how can I get it to stop after the first string matched?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对此做了很多实验,我尝试过的所有技术中最好的方法是使用正则表达式:
I've done a lot of experimentation with this, and the best approach of all the techniques I tried is to use regex:
它已经停在
$arrAgent
中匹配的第一个字符串处;这就是循环内的break
所做的事情。如果您想优先考虑与
ipad
的比赛而不是与mobile
的比赛,只需重新排列您的$arrAgent
即可'ipad'
> 出现在'mobile'
之前。如果您确保数组的开头是特定术语,结尾是通用术语,那么您将始终获得返回的最具体的可能匹配。
It will already stop at the first string in
$arrAgent
that matches; that's what thebreak
inside the loop is doing.If you want to prioritize a match against
ipad
over one againstmobile
, just rearrange your$arrAgent
so'ipad'
appears before'mobile'
.If you make sure the array is arranged with specific terms at the beginning and generic terms at the end, you'll always get the most specific possible match returned.
为什么不先检查一下它是否是 iPad 呢?
像这样的东西:
Why don't you simply first check if it's an iPad?
Something like this: