Bug #70233 [NEW]: \K with preg_replace not working as expected
| From: | jonny5 at quantentunnel dot de | Date: | Tue, 11 Aug 2015 05:16:35 +0000 |
| Subject: | Bug #70233 [NEW]: \K with preg_replace not working as expected | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-195102@lists.php.net to get a copy of this message | ||
From: jonny5 at quantentunnel dot de
Operating system:
PHP version: 5.4.44
Package: PCRE related
Bug Type: Bug
Bug description:\K with preg_replace not working as expected
Description:
------------
In certain cases, when using \K escape sequence (reset beginning of
reported match) with preg_replace, result is not as expected. Every
other match seems to be skipped.
The test-pattern (?: |\G)\d\B\K is to separate digits of numbers
preceded by start or space with *. This pattern works as expected e.g.
in regex101 pcre-mode: https://regex101.com/r/gF7uB7/1
but not with
preg_replace (at least with PHP 5.5.14) where it seems to skip every
other zero-width match: https://eval.in/415050
Test script:
---------------
<?
$pattern = '~(?: |\G)\d\B\K~';
$replace = "*";
$str = "123 a123 1234567 b123";
echo preg_replace($pattern, $replace, $str);
?>
Expected result:
----------------
1*2*3 a123 1*2*3*4*5*6*7 b123
Actual result:
--------------
1*23 a123 1*23*45*67 b123
--
Edit bug report at https://bugs.php.net/bug.php?id=70233&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70233&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70233&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70233&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=70233&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=70233&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=70233&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=70233&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=70233&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=70233&r=support
Expected behavior: https://bugs.php.net/fix.php?id=70233&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=70233&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=70233&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=70233&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70233&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=70233&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=70233&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=70233&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=70233&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=70233&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=70233&r=mysqlcfg