Bug #70233 [Dup]: \K with preg_replace not working as expected

From: Date: Tue, 11 Aug 2015 05:49:50 +0000
Subject: Bug #70233 [Dup]: \K with preg_replace not working as expected
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-195104@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70233&edit=1 ID: 70233 User updated by: jonny5 at quantentunnel dot de Reported by: jonny5 at quantentunnel dot de Summary: \K with preg_replace not working as expected Status: Duplicate Type: Bug Package: PCRE related PHP Version: 5.4.44 Block user comment: N Private report: N New Comment: Yes, I didn't see the bug was submitted already when writing my report! Thanks Previous Comments: ------------------------------------------------------------------------ [2015-08-11 05:28:27] requinix@php.net I take it bug #70232 is a more detailed version of this one? ------------------------------------------------------------------------ [2015-08-11 05:16:33] jonny5 at quantentunnel dot de 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 this bug report at https://bugs.php.net/bug.php?id=70233&edit=1

« previous php.bugs (#195104) next »