Regular expression to detect Internet Explorer 11

ぃ、小莉子 提交于 2019-12-19 05:54:22

问题


I am using this preg_match string

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".

How can I do this without PHP complaining at me?


回答1:


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 .

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.

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




回答2:


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

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):

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.';
}



回答3:


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




回答4:


IE11 has touch versions and non-touch versions, this means that the Guilherme Sehn's answer will not work for touch devices.

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.

Likewise if you want to check if they're on touch just do a strpos for "Touch;".




回答5:


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 stristr function to see if a string occurs in another one

stristr($_SERVER['HTTP_USER_AGENT'],'Trident/7.0; rv:11.0')



回答6:


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:

$regexp = sprintf( '/%s/', preg_quote( 'Trident/7.0; rv:11.0', '/' ) );
preg_match( $regexp, $_SERVER['HTTP_USER_AGENT'] );

http://www.php.net/manual/en/function.preg-quote.php




回答7:


The / after Trident is being treated as a delimiter. You could escape it:

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:

if (false !== strpos($_SERVER["HTTP_USER_AGENT"], '/Trident/7.0; rv:11.0/')) {



回答8:


I would use the browscap project to get this data myself. It's faster and more reliable



来源:https://stackoverflow.com/questions/19742965/regular-expression-to-detect-internet-explorer-11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!