Is there a fool-proof regex (I'm using PHP) to match the Microsoft Edge user agent string?
Asked
Active
Viewed 6,940 times
5
-
[Which one](http://stackoverflow.com/questions/30591706/what-is-the-user-agent-string-name-for-microsoft-edge)? – Wiktor Stribiżew Aug 11 '15 at 19:53
-
1`preg_match("/Edge\/12\./", $_SERVER['HTTP_USER_AGENT'], $output_array);` – cmorrissey Aug 11 '15 at 19:54
-
I'm curious -- what would the use case be? – Dave Voyles Aug 12 '15 at 22:25
-
Nothing special, just playing around. – GluePear Aug 13 '15 at 09:11
-
/(edge)[ \/]([\w.]+)/ – Codebeat Aug 22 '18 at 18:02
2 Answers
9
As well most browsers you can simply just say the browser name in the comparison string as shown below.
$user_agent = $_SERVER['HTTP_USER_AGENT'];
preg_match('/Edge/i', $user_agent)
This worked for me. Hope it does for you too.

Kevin Kennedy
- 114
- 1
3
I would HIGHLY recommend
- not using us-sniffing. There is almost no reason to do so.
- if you absolutely really have to, using a library like ua-parser rather than rolling your own.

Patrick
- 13,872
- 5
- 35
- 53
-
11. Working around fun browser bugs is a good reason. 2. A library vs. one line of code, guess it depends on how accurate the match has to be. – Leia Apr 12 '17 at 00:50
-
1. feature detecting the bug is 100% more reliable than ua sniffing. 2. the one line of code will almost always be worse, long term – Patrick Apr 12 '17 at 01:52
-
The issue I had was with setting a header for redirects. MSIE display unicode characters in the redirected URL nicely if its not encoded, Edge needs it to be encoded. Not sure how to future detect in backend: `header("LOCATION: /".(preg_match("/Edge\/1(2|3|4)\./", getenv('HTTP_USER_AGENT'))) ? rawurlencode("sök") : "sök");` – Leia Apr 12 '17 at 06:54
-
that is a 100% valid case for ua sniffing. but that detect literally breaks yesterday because of the release of Edge 15. using a ua parsing library would prevent that. That being said, if you can create a sample case that shows this behavior (either static file or httpbin or something), I work on Edge, I can help get it fixed. – Patrick Apr 13 '17 at 07:52
-
The issue in question is fixed (EdgeHTML issue tracker #8477507), the regex only targets versions of Edge that's affected by the bug... ;-) Again, it depends on how accurate the detection needs to be and the purpose of the sniff. A good rule of thumb, user agent sniffing is **always** unreliable so never trust the information and always have an fallback in place. – Leia Apr 13 '17 at 13:09