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
 [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: Fri Nov 08 13:01:28 2024 UTC