0

I am checking browser agent and version using preg match. but i found error while i update my php version 5.3 to 5.4

preg_match( '/Mozilla/([0-9].[0-9]{1,2})/',$HTTP_USER_AGENT,$log_version)

have any idea how to fix this issue?

Uttam Panara
  • 541
  • 2
  • 10
  • 28
  • 3
    escape backslash, because it's a special regex operator `'/Mozilla\/([0-9].[0-9]{1,2})/'` – Agnius Vasiliauskas Apr 27 '18 at 06:19
  • i have same issue in preg_match('/Netscape([0-9])/([0-9].[0-9]{1,2})/',$HTTP_USER_AGENT,$log_version1). so that i need to add \ before second / like this preg_match('/Netscape([0-9])\/([0-9].[0-9]{1,2})/',$HTTP_USER_AGENT,$log_version1) – Uttam Panara Apr 27 '18 at 06:27
  • Read about [regular expression pattern delimiters](http://php.net/manual/en/regexp.reference.delimiters.php). Your `regex` contains a delimiter in the middle of the pattern. – axiac Apr 27 '18 at 06:39
  • @AgniusVasiliauskas there is no backslash (`\ `) in the `regex` posted in the question. – axiac Apr 27 '18 at 06:41

1 Answers1

3

Apply \ (backslash) before second /(forwardslash) to escape it

preg_match( '/Mozilla\/([0-9].[0-9]{1,2})/',$HTTP_USER_AGENT,$log_version);

preg_match('/Netscape([0-9])\/([0-9].[0-9]{1,2})/',$HTTP_USER_AGENT,$log_version1);

Note:- Any / in between starting and ending /(delementer) in preg_match() need to be escaped to make it run fine.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98