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
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')
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;".
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
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.';
}
I would use the browscap project to get this data myself. It's faster and more reliable
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.