Edit report at https://bugs.php.net/bug.php?id=80652&edit=1
ID: 80652
Updated by: requinix@php.net
Reported by: greg at subaqua dot co dot uk
-Summary: Regex delimiters not validated correctly
+Summary: Leading whitespace in a regex is ignored
Status: Open
-Type: Bug
+Type: Documentation Problem
Package: PCRE related
Operating System: n/a
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Leading whitespace in a pattern is ignored. The delimiters thus are the asterisks, and the pattern
splits on comma + space.
A quick comment in the docs regarding the leading whitespace sounds reasonable.
Previous Comments:
------------------------------------------------------------------------
[2021-01-21 13:13:11] greg at subaqua dot co dot uk
Description:
------------
The documentation at https://www.php.net/manual/en/regexp.reference.delimiters.php
says that:
When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A
delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.
The validation for delimiters works in *most* cases and gives a warning such as:
Warning: preg_split(): Delimiter must not be alphanumeric or backslash in php shell code on line 1
However, it fails to detect the invalid delimiters in the string " *, *".
Instead, it runs without error/warning and gives output based on a substring(?) of the supplied
regex.
According to https://3v4l.org/DdBp1 it affects all versions of
PHP from 4.3 to 8.0
Test script:
---------------
# Valid delimiters - PASS
php > var_export(preg_split('/ *, */', 'a , b'));
array (
0 => 'a',
1 => 'b',
)
# Invalid delimiters - PASS - gives warning as expected
php > var_dump(preg_split('X *, *Y', 'a , b'));
Warning: preg_split(): Delimiter must not be alphanumeric or backslash in php shell code on line 1
bool(false)
# Invalid delimiters - FAIL - should warn about either mismatched or invalid delimiters.
# Instead, it gives no warning and unexpected output.
php > var_export(preg_split(' *, *', 'a , b'));
array (
0 => 'a ',
1 => 'b',
)
Expected result:
----------------
I expect the invalid delimiters to trigger a warning.
Actual result:
--------------
The invalid delimiters are silently ignored.
The output does not correspond to the supplied regex.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80652&edit=1