用于检测Internet Explorer 11的正则表达式 [英] Regular expression to detect Internet Explorer 11

查看:103
本文介绍了用于检测Internet Explorer 11的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用preg_match字符串

preg_match('/Trident/7.0; rv:11.0/',$_SERVER["HTTP_USER_AGENT"]

以检测IE11,因此可以为其启用平板电脑模式.但是,它返回"unknown delimiter 7".

to detect IE11 so I can enable tablet mode for it. However it returns "unknown delimiter 7".

在没有PHP抱怨的情况下如何做到这一点?

How can I do this without PHP complaining at me?

推荐答案

这真的是正则表达式还是只是文字字符串?如果只是字符串,则可以使用 strpos 函数.

Is this really a regular expression or just a literal string? If it's just a string, you can use the strpos function instead.

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 .

您可以在此答案的注释中看到,该代码无法在所有情况下正确检测IE 11.我并没有真正测试它是否匹配它,我只是将问题创建者的代码调整为使用strpos而不是preg_match,因为它的应用方式不正确.

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,请查看其他答案.

If you want a reliable way to detect IE 11 please take a look at the other answers.

这篇关于用于检测Internet Explorer 11的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆