php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38368 Match-all-or-none pattern in preg_replace duplicates repl. text
Submitted: 2006-08-07 15:02 UTC Modified: 2006-11-16 18:14 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: bloudon at townnews dot com Assigned: andrei (profile)
Status: Not a bug Package: PCRE related
PHP Version: 5CVS-2006-08-07 (snap) OS: Slackware Linux 10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bloudon at townnews dot com
New email:
PHP Version: OS:

 

 [2006-08-07 15:02 UTC] bloudon at townnews dot com
Description:
------------
When using preg_replace with a pattern of '/(.*)/' or similar, the returned string will contain two instances of the replacement text, with the second instance appended to the first and given empty backreferences.

This behavior does not occur when the pattern is anchored to the start of the string: '/^(.*)/'.

./configure --with-apxs --enable-versioning --enable-memory-limit --with-mysql=/usr --enable-xslt --with-xslt-sablot --with-gd --with-jpeg-dir=/usr --with-zlib-dir=/usr --with-ttf=/usr --with-freetype-dir=/usr --enable-gd-native-ttf --enable-bcmath --with-mime-magic

Reproduce code:
---------------
<?php
echo preg_replace('/(.*)/', 'a$1b', 'foo') ."\n";
echo preg_replace('/(.*)$/', 'a$1b', 'foo') ."\n";
echo preg_replace('/^(.*)/', 'a$1b', 'foo') ."\n";
echo preg_replace('/^(.*)$/', 'a$1b', 'foo') ."\n";
echo preg_replace('/.*/', 'ab', 'foo') ."\n";
echo preg_replace('/f*/', 'ab', 'foo') ."\n";
?>

Expected result:
----------------
afoob
afoob
afoob
afoob
ab
aboo

Actual result:
--------------
afoobab
afoobab
afoob
afoob
abab
ababoaboab


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-11-16 18:14 UTC] nlopess@php.net
not a problem in PHP surely. And I would say this is expected (at least the master Friedl convinved me so), as the engine will do a second attempt to match and will indeed match the empty string (because of the *)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 23:01:28 2024 UTC