Edit report at http://bugs.php.net/bug.php?id=52118&edit=1
ID: 52118 Updated by: sala...@php.net Reported by: tomasz dot slominski at gmail dot com Summary: preg_replace gives bad output if matching group equals whole string -Status: Open +Status: Bogus Type: Bug Package: *Regular Expressions Operating System: WIN XP SP3 PHP Version: Irrelevant New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php This is expected behaviour. When a match is found, PCRE checks for the next possible match starting at the point immediately after the previous match. In your case, the .* first matches the entire subject string "test", then also matches again at very end of the string since the * quantifier allows matching nothing. Previous Comments: ------------------------------------------------------------------------ [2010-06-18 11:40:13] tomasz dot slominski at gmail dot com Fast hack: var_dump(preg_replace(array("/(.+)(.*)/"), array('!$1$2'),'test')); gives good output (!test) ------------------------------------------------------------------------ [2010-06-18 11:25:37] tomasz dot slominski at gmail dot com Description: ------------ preg replace is going mad when matching group equals to (.*). It seems that substitution is made 2 times instead of 1. Test script: --------------- var_dump(preg_replace(array("/(.*)/"), array('!$1'),'test')); var_dump(preg_replace(array("/(.*)/"), array('$1!'),'test')); var_dump(preg_replace(array("/(.*)/"), array('!$1!'),'test')); Expected result: ---------------- string '!test' (length=5) string 'test!' (length=5) string '!test!' (length=6) Actual result: -------------- string '!test!' (length=6) string 'test!!' (length=6) string '!test!!!' (length=8) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=52118&edit=1