php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53855 preg_replace doing double replacement
Submitted: 2011-01-27 14:58 UTC Modified: 2011-01-27 15:07 UTC
From: richard at roguewavelimited dot com Assigned:
Status: Not a bug Package: Regexps related
PHP Version: 5.3.3 OS: Mac OSX 10.6.6
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: richard at roguewavelimited dot com
New email:
PHP Version: OS:

 

 [2011-01-27 14:58 UTC] richard at roguewavelimited dot com
Description:
------------
The preg_replace is doing a double replacement when the parenthesized pattern is the whole string.

Test script:
---------------
define ('EOL', "\n");

$patterns = '|(.*)|';
$replacements = '$1-new';
$line = 'OLD';

$line = @preg_replace($patterns, $replacements, $line, -1, $changes);
print $line . EOL;

Expected result:
----------------
'OLD-new'



Actual result:
--------------
'OLD-new-new'

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-27 15:00 UTC] richard at roguewavelimited dot com
-PHP Version: 5.3.5 +PHP Version: 5.3.3
 [2011-01-27 15:00 UTC] richard at roguewavelimited dot com
Correct php version
 [2011-01-27 15:07 UTC] salathe@php.net
-Status: Open +Status: Bogus
 [2011-01-27 15:07 UTC] salathe@php.net
This is expected behaviour and nothing peculiar to PHP.  The * quantifier allows 
an "empty" match to occur at the end of your subject string.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 02 18:01:31 2024 UTC