Regular expression to detect Internet Explorer 11

后端 未结 8 1826
逝去的感伤
逝去的感伤 2021-01-11 18:43

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

8条回答
  •  北海茫月
    2021-01-11 19:39

    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.

提交回复
热议问题