|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77827 preg_match does not ignore \r in regex flags
Submitted: 2019-03-30 17:49 UTC Modified: 2019-03-31 11:13 UTC
From: Assigned: cmb (profile)
Status: Closed Package: PCRE related
PHP Version: 7.3.3 OS: Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
New email:
PHP Version: OS:


 [2019-03-30 17:49 UTC]
When running the repro script on my machine I am hitting two issues:

1. preg_match is unable to handle my valid expression
2. it outputs a broken error message

This only happens on my (Windows) machine. On 3v4l ( it's working like it should.

Test script:

$pattern = '
                (?:(?P<table1>.+)(?:\.))?(?P<column1>[^ ]+)
            (?:(?P<table2>.+)(?:\.))?(?P<column2>[^ ]+)

var_dump(preg_match($pattern, 'id'));

Expected result:
string(16) "10.32 2018-09-10"

Actual result:
string(16) "10.32 2018-09-10"
' in \path\to\php-bug.php on line 17


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-03-30 19:37 UTC]
Weirdly enough when I use a single line pattern it starts working:


$pattern = '~^(?:(?:(?P<function>function)\s*\(\s*(?:(?P<table1>.+)(?:\.))?(?P<column1>[^ ]+)\s*\))|(?:(?P<table2>.+)(?:\.))?(?P<column2>[^ ]+))(?:\s+as\s+(?P<alias>.+))?$~ix';

var_dump(preg_match($pattern, 'id'));


string(16) "10.32 2018-09-10"

Am I doing something wrong here in combination with the platform I am on somehow?
 [2019-03-30 19:43 UTC]
Works for my 7.3.3 on Windows. Does it work if you copy/paste the code from this bug report? Is there maybe a stray unprintable character in your file?
 [2019-03-30 20:16 UTC]
Nope. Same result. When I check a hex dump of my file I also do not see anything strange at first sight.
 [2019-03-30 20:32 UTC]
-Summary: preg_match throws error on valid expression +Summary: preg_match does not ignore \r in regex flags -Status: Open +Status: Verified
 [2019-03-30 20:32 UTC]
Found it. The problem is the line ending in the flags part: PHP will ignore \n but not \r, then complain about it as an invalid flag.

When I tested, I had created the file with LF line endings.

What's more, the error message is confusing because it says this (with Xdebug) in cmd, where the \r causes a carriage return and so overwrites the rest of the message:

  string(16) "10.32 2018-09-10"

  ' in C:\Users\xxxxxx\t.php on line 17er '

  Call Stack:
      0.4049     397800   1. {main}() C:\Users\xxxxxx\t.php:0
      0.4051     397832   2. preg_match() C:\Users\xxxxxx\t.php:17


The error message was actually

  Warning: preg_match(): Unknown modifier ''

with an \r between the apostrophes.
 [2019-03-30 20:44 UTC]
Awesome! Thanks for digging into it and confirming I am not going entirely crazy yet.
 [2019-03-31 11:13 UTC]
-Assigned To: +Assigned To: cmb
 [2019-03-31 11:37 UTC]
Automatic comment on behalf of
Log: Fix #77827: preg_match does not ignore \r in regex flags
 [2019-03-31 11:37 UTC]
-Status: Verified +Status: Closed
 [2019-03-31 11:38 UTC]
Automatic comment on behalf of
Log: Fix #77827: preg_match does not ignore \r in regex flags
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Sep 24 22:03:37 2021 UTC