Bug #77827 [Opn->Ver]: preg_match does not ignore \r in regex flags

From: Date: Sat, 30 Mar 2019 20:32:50 +0000
Subject: Bug #77827 [Opn->Ver]: preg_match does not ignore \r in regex flags
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-220261@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=77827&edit=1 ID: 77827 Updated by: requinix@php.net Reported by: peehaa@php.net -Summary: preg_match throws error on valid expression +Summary: preg_match does not ignore \r in regex flags -Status: Open +Status: Verified Type: Bug Package: PCRE related Operating System: Windows PHP Version: 7.3.3 Block user comment: N Private report: N New Comment: 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. https://github.com/php/php-src/blob/PHP-7.3.3/ext/pcre/php_pcre.c#L696 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 C:\Users\xxxxxx\t.php:17: bool(false) C:\Users\xxxxxx\t.php:18: int(1) The error message was actually Warning: preg_match(): Unknown modifier '' with an \r between the apostrophes. Previous Comments: ------------------------------------------------------------------------ [2019-03-30 20:16:47] peehaa@php.net 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 19:43:03] requinix@php.net 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 19:37:24] peehaa@php.net Weirdly enough when I use a single line pattern it starts working: <?php $pattern = '~^(?:(?:(?P<function>function)\s*\(\s*(?:(?P<table1>.+)(?:\.))?(?P<column1>[^ ]+)\s*\))|(?:(?P<table2>.+)(?:\.))?(?P<column2>[^ ]+))(?:\s+as\s+(?P<alias>.+))?$~ix'; var_dump(PCRE_VERSION); var_dump(preg_match($pattern, 'id')); var_dump(preg_last_error()); ----- string(16) "10.32 2018-09-10" int(1) int(0) Am I doing something wrong here in combination with the platform I am on somehow? ------------------------------------------------------------------------ [2019-03-30 17:49:26] peehaa@php.net Description: ------------ 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 (https://3v4l.org/hdiM4) it's working like it should. Test script: --------------- <?php $pattern = ' ~^ (?: (?:(?P<function>function)\s*\(\s* (?:(?P<table1>.+)(?:\.))?(?P<column1>[^ ]+) \s*\)) | (?:(?P<table2>.+)(?:\.))?(?P<column2>[^ ]+) ) (?:\s+as\s+(?P<alias>.+))? $~ix '; var_dump(PCRE_VERSION); var_dump(preg_match($pattern, 'id')); var_dump(preg_last_error()); Expected result: ---------------- string(16) "10.32 2018-09-10" int(1) int(0) Actual result: -------------- string(16) "10.32 2018-09-10" ' in \path\to\php-bug.php on line 17 bool(false) int(1) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=77827&edit=1

« previous php.bugs (#220261) next »