Bug #77334 [Com]: Issue with preg_replace shorthand character in character class
| From: | danack@php.net | Date: | Fri, 21 Dec 2018 17:17:57 +0000 |
| Subject: | Bug #77334 [Com]: Issue with preg_replace shorthand character in character class | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-218553@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=77334&edit=1
ID: 77334
Comment by: danack@php.net
Reported by: webdev at westernseminary dot edu
Summary: Issue with preg_replace shorthand character in
character class
Status: Open
Type: Bug
Package: *Regular Expressions
Operating System: Windows Server 2012
PHP Version: 7.3.0
Block user comment: N
Private report: N
New Comment:
You might want to check the error message settings on your code. That code generates an error for me
on PHP 7.3.0: https://3v4l.org/NuLqI
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in
/in/NuLqI on line 3
Which explains why it's not working.
This is possibly related to the changed in the preg library used in PHP 7.3, but I'm on a
terrible network connection so hard to find the relevant link.
Escaping the hyphen makes it behave how you're expecting it to behave.
echo preg_replace('/[^\d\-\.]/', '', '123-abc');
Previous Comments:
------------------------------------------------------------------------
[2018-12-21 16:58:01] webdev at westernseminary dot edu
Description:
------------
When a shorthand character (\d) is used in a character class ([\d]) along wiht a dash and a dot, the
regular expression throws an error in PHP 7.3.0 even though the regular expression is valid. The
error does not occur in PHP 7.2.13.
Test script:
---------------
// SHORT EXAMPLE
echo preg_replace('/[^\d-\.]/', '', '123abc');
// LONG EXAMPLE
$value = '123abc';
$rgxs = array(
'/[\d]/',
'/[\d-]/',
'/[\d\.]/',
'/[\d-\.]/', //bug
'/[\d\.-]/',
'/[^\d]/',
'/[^\d-]/',
'/[^\d\.]/',
'/[^\d-\.]/', //bug
'/[^\w-\.]/', //bug
'/[^0-9-\.]/',
);
echo '<table border="1" cellpadding="10">';
echo '<tr>';
echo '<th>Index</th>';
echo '<th>Regular Expression</th>';
echo '<th>Result</th>';
echo '<th>Last Error</th>';
echo '</tr>';
foreach($rgxs as $i => $rgx) {
echo '<tr>';
echo '<td>'.$i.'</td>';
echo '<td>'.$rgx.'</td>';
echo '<td>'.preg_replace($rgx, '', $value).'</td>';
echo '<td>'.preg_last_error().'</td>';
echo '</tr>';
}
echo '</table>';
Expected result:
----------------
For the short example, the expected result would be "123".
Actual result:
--------------
For the short example, the actual result is NULL.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77334&edit=1