From: stuge-phpbugs at cdy dot org Operating system: Linux PHP version: 4.3.1 PHP Bug Type: *Regular Expressions Bug description: [ep]reg_replace "z?$" with "a" in "xyz" yields "xyaa" instead of "xya"
Verified with 4.3.0 and 4.3.2rc2 but haven't tried 4.3.2. NEWS for 4.3.2 do not mention any *reg_replace changes. Reproducible with eregi_replace(). <?php $f="xyz"; $g=ereg_replace("z?$","a",$f); $h=preg_replace("/z?$/","a",$f); print $f; /* xyz */ print " ".$g; /* xyaa, I want xya */ print " ".$h; /* xyaa, I want xya */ ?> At least in CVS php4/ext/standard/reg.c I believe the problem is that regexec() on line 306 will be called a second time after the first iteration has already matched to the end of the string. Matching from the end of the string should only be allowed on the very first iteration. If this analysis is correct, the following patch might work, but I haven't tried it. --- reg.c.org 2003-06-02 05:23:51.000000000 +0200 +++ reg.c 2003-06-02 05:26:06.000000000 +0200 @@ -302,7 +302,7 @@ err = pos = 0; buf[0] = '\0'; - while (!err) { + do { err = regexec(&re, &string[pos], re.re_nsub+1, subs, (pos ? REG_NOTBOL : 0)); if (err && err != REG_NOMATCH) { @@ -396,7 +396,7 @@ /* stick that last bit of string on our output */ strcat(buf, &string[pos]); } - } + } while(!err && string[pos]); /* don't want to leak memory .. */ efree(subs); -- Edit bug report at http://bugs.php.net/?id=23946&edit=1 -- Try a CVS snapshot: http://bugs.php.net/fix.php?id=23946&r=trysnapshot Fixed in CVS: http://bugs.php.net/fix.php?id=23946&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=23946&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=23946&r=needtrace Try newer version: http://bugs.php.net/fix.php?id=23946&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=23946&r=support Expected behavior: http://bugs.php.net/fix.php?id=23946&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=23946&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=23946&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=23946&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=23946&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=23946&r=dst IIS Stability: http://bugs.php.net/fix.php?id=23946&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=23946&r=gnused