Doc #80652 [Opn->Csd]: Leading whitespace in a regex is ignored
Edit report at https://bugs.php.net/bug.php?id=80652&edit=1
ID: 80652
Updated by: cmb@php.net
Reported by: greg at subaqua dot co dot uk
Summary: Leading whitespace in a regex is ignored
-Status: Open
+Status: Closed
Type: Documentation Problem
Package: PCRE related
Operating System: n/a
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=7962c3e6a2af22dbc568e3cd117287d8baec8e80
Log: Fix #80652: Leading whitespace in a regex is ignored
Previous Comments:
------------------------------------------------------------------------
[2021-01-21 13:30:45] requinix@php.net
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.
------------------------------------------------------------------------
[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
Thread (2 messages)