Bug #16786 Updated: ereg_replace substitution behaving badly

From: Date: Tue, 21 May 2002 15:41:10 +0000
Subject: Bug #16786 Updated: ereg_replace substitution behaving badly
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-8099@lists.php.net to get a copy of this message
ID: 16786 Updated by: rasmus@php.net Reported By: tom@scl.co.uk -Status: Feedback +Status: Closed Bug Type: Regexps related Operating System: Linux 2.2.19 PHP Version: 4.2.0 New Comment: Seems to be fixed in 4.2.1 and HEAD Previous Comments: ------------------------------------------------------------------------ [2002-04-24 07:25:10] tom@scl.co.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-04-24 06:30:42] 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 05:12:16] tom@scl.co.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? ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=16786&edit=1

« previous php.bugs (#8099) next »