php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70233 \K with preg_replace not working as expected
Submitted: 2015-08-11 05:16 UTC Modified: 2015-08-11 05:49 UTC
From: jonny5 at quantentunnel dot de Assigned:
Status: Duplicate Package: PCRE related
PHP Version: 5.4.44 OS:
Private report: No CVE-ID: None
 [2015-08-11 05:16 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-11 05:28 UTC] requinix@php.net
-Status: Open +Status: Duplicate
 [2015-08-11 05:28 UTC] requinix@php.net
I take it bug #70232 is a more detailed version of this one?
 [2015-08-11 05:49 UTC] jonny5 at quantentunnel dot de
Yes, I didn't see the bug was submitted already when writing my report! Thanks
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 07:01:29 2024 UTC