Search code examples
phppreg-matchinternet-explorer-11

Regular expression to detect Internet Explorer 11


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?


Solution

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