5

Is there a fool-proof regex (I'm using PHP) to match the Microsoft Edge user agent string?

GluePear
  • 7,244
  • 20
  • 67
  • 120

2 Answers2

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.

3

I would HIGHLY recommend

  1. not using us-sniffing. There is almost no reason to do so.
  2. if you absolutely really have to, using a library like ua-parser rather than rolling your own.
Patrick
  • 13,872
  • 5
  • 35
  • 53
  • 1
    1. 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