|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-12-21 16:58 UTC] 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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 06:00:01 2025 UTC |
Yes. However, should it throw an error? Isn't that a valid regular expression? Maybe there is a bug in the new preg library? If I swap the shorthand \d with 0-9, it works fine without throwing an error. echo preg_replace('/[^0-9-\.]/', '', '123abc');