php 用于检测 Internet Explorer 11 的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19742965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular expression to detect Internet Explorer 11
提问by ben
I am using this preg_match
string
我正在使用这个preg_match
字符串
preg_match('/Trident/7.0; rv:11.0/',$_SERVER["HTTP_USER_AGENT"]
to detect IE11 so I can enable tablet mode for it. However it returns "unknown delimiter 7".
检测 IE11,以便我可以为其启用平板电脑模式。但是它返回“未知分隔符 7”。
How can I do this without PHP complaining at me?
如果 PHP 不向我抱怨,我该如何做到这一点?
回答by Guilherme Sehn
Is this really a regular expression or just a literal string? If it's just a string, you can use the strposfunction instead.
这真的是一个正则表达式还是只是一个文字字符串?如果它只是一个字符串,则可以改用strpos函数。
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false) {
// your code
}
If it's a regular expression, you must escape special characters like /
and .
如果是正则表达式,则必须转义特殊字符,例如/
和.
EDIT:
编辑:
You can see in the comments of this answer that the code doesn't detect IE 11 properly in all cases. I didn't actually test it to match it, I've just adjusted the code of the question creator to use strpos
instead of preg_match
because it was applied incorrectly.
您可以在此答案的评论中看到,该代码在所有情况下都无法正确检测 IE 11。我没有实际测试它以匹配它,我只是调整了问题创建者的代码以使用strpos
而不是preg_match
因为它应用不正确。
If you want a reliable way to detect IE 11 please take a look at the other answers.
如果您想要一种可靠的方法来检测 IE 11,请查看其他答案。
回答by Ken Pega
User-agent string may be reported differently across various platforms or devices. Take a look at this: http://msdn.microsoft.com/en-au/library/ie/hh869301(v=vs.85).aspx
用户代理字符串可能会在各种平台或设备上以不同的方式报告。看看这个:http: //msdn.microsoft.com/en-au/library/ie/hh869301(v=vs.85).aspx
Just found the following string pattern covering most of the User Agents as reported for IE11 or above (It may not hold true if Microsoft changes its User-agent string again for newer version of IE):
刚刚发现以下字符串模式涵盖了针对 IE11 或更高版本报告的大多数用户代理(如果 Microsoft 为较新版本的 IE 再次更改其用户代理字符串,则可能不成立):
if (preg_match("/(Trident\/(\d{2,}|7|8|9)(.*)rv:(\d{2,}))|(MSIE\ (\d{2,}|8|9)(.*)Tablet\ PC)|(Trident\/(\d{2,}|7|8|9))/", $_SERVER["HTTP_USER_AGENT"], $match) != 0) {
print 'You are using IE11 or above.';
}
回答by Kubas
I think it should be
我认为应该是
if (preg_match("/Trident\/7.0;(.*)rv:11.0/", $_SERVER["HTTP_USER_AGENT"], $match) != 0) {}
because sometimes you can see the user agent like this one
因为有时你可以看到这样的用户代理
Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; ASU2JS; rv:11.0) like Gecko
Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; ASU2JS; rv:11.0) 像 Gecko
回答by userqwert
IE11 has touch versions and non-touch versions, this means that the Guilherme Sehn's answer will not work for touch devices.
IE11 有触控版和非触控版,这意味着 Guilherme Sehn 的答案不适用于触控设备。
if(strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false)
Instead, use this:
相反,使用这个:
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'rv:11.0') !== false
&& strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0;')!== false)
{
echo "User is on IE11 touch / non-touch";
}
This is because any number of parameters could be added between "Trident" and the revision number.
这是因为可以在“Trident”和修订号之间添加任意数量的参数。
Likewise if you want to check if they're on touch just do a strpos for "Touch;".
同样,如果您想检查他们是否处于接触状态,只需为“Touch;”做一个 strpos。
回答by Can YILDIZ
It's always good to use another character which is not in regexp string so you don't have to escape your string and easier to read for long ones. So you may use ~
for delimiter:
使用另一个不在正则表达式字符串中的字符总是好的,这样你就不必转义你的字符串并且更容易阅读长字符串。所以你可以使用~
分隔符:
preg_match('~Trident/7.0; rv:11.0~',$_SERVER["HTTP_USER_AGENT"])
Another idea is to use stristrfunction to see if a string occurs in another one
另一个想法是使用stristr函数来查看一个字符串是否出现在另一个字符串中
stristr($_SERVER['HTTP_USER_AGENT'],'Trident/7.0; rv:11.0')
回答by feeela
You need to escape the middle slash /
to work in a regular expression that is enclosed by slahses:
您需要转义中间的斜杠/
才能在由斜杠括起来的正则表达式中工作:
preg_match( '/Trident\/7.0; rv:11.0/', $_SERVER['HTTP_USER_AGENT'] );
You could also use preg_quote()
to escape all special characters in the string:
您还可以使用preg_quote()
转义字符串中的所有特殊字符:
$regexp = sprintf( '/%s/', preg_quote( 'Trident/7.0; rv:11.0', '/' ) );
preg_match( $regexp, $_SERVER['HTTP_USER_AGENT'] );
回答by lonesomeday
The /
after Trident
is being treated as a delimiter. You could escape it:
在/
后Trident
正在治疗作为分隔符。你可以逃避它:
preg_match('/Trident\/7.0; rv:11.0/',$_SERVER["HTTP_USER_AGENT"]
Better, however, would be not to use preg_match
: you don't need it. All you're doing is looking for an invariant substring, which can be done with strpos
:
但是,最好不要使用preg_match
:您不需要它。您所做的只是寻找一个不变的子字符串,这可以通过以下方式完成strpos
:
if (false !== strpos($_SERVER["HTTP_USER_AGENT"], '/Trident/7.0; rv:11.0/')) {
回答by Machavity
I would use the browscap projectto get this data myself. It's faster and more reliable
我会使用browscap 项目自己获取这些数据。它更快更可靠