php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16786 ereg_replace substitution behaving badly
Submitted: 2002-04-24 05:12 UTC Modified: 2002-05-21 11:41 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: tom at scl dot co dot uk Assigned:
Status: Closed Package: Regexps related
PHP Version: 4.2.0 OS: Linux 2.2.19
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: tom at scl dot co dot uk
New email:
PHP Version: OS:

 

 [2002-04-24 05:12 UTC] tom at scl dot co dot uk
Hi,
I have just downloaded PHP4.0.2 and several pieces of code broke because ereg_replace() seemed to be working incorrectly:

if for example I did the following:

$str = "word1-word2";
$str = ereg_replace("^([^-]*)-(.*)", '\1+\2', $str);

it works as expected, ($str == "word1+word2")

The problem arises if one of the parenthasized subexpression is empty the \x (where x is the number of the subexpression) in the replace string does not get replaced at all, instead of with the empty string eg:

$str = "-word2";
$str = ereg_replace("^([^-]*)-(.*)", '\1+\2', $str);

for this example I'd expect the result to be ($str == "+word2") but instead you get ($str == "\1+word2") because \1 is empty.

I don't believe that this is meant to happen but if it is how are we meant to deal with such a case?

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-04-24 06:30 UTC] yohgaki@php.net
Could you paste complete code?

(With complete code, we can test immediately and put the code
to test suite, if it's needed)


 [2002-04-24 07:25 UTC] tom at scl dot co dot uk
This code should demonstrate the problem:

<?php
$str = "word1-word2";
$str = ereg_replace("^([^-]*)-(.*)", '\1+\2', $str);
print("1) Result = '$str'\n");

$str = "-word2";
$str = ereg_replace("^([^-]*)-(.*)", '\1+\2', $str);
print("2) Result = '$str'\n");

if ($str == "+word2") print ("No Bug Detected\n");
else print("Bug Detected (The result should be '+word2')\n");
?>
 [2002-05-21 11:41 UTC] rasmus@php.net
Seems to be fixed in 4.2.1 and HEAD
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 12:01:27 2024 UTC