|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-06-18 11:25 UTC] 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)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 04 04:00:02 2025 UTC |
Fast hack: var_dump(preg_replace(array("/(.+)(.*)/"), array('!$1$2'),'test')); gives good output (!test)ok, but shouldn't greedy .* consume the whole string? var_dump(preg_replace("/(.*)/U", '$1!','test')); gives string '!t!!e!!s!!t!!' (length=13) and that's ok, but why var_dump(preg_replace("/(.*)/", '$1!','test')); is producing string 'test!!' (matching 'test' - nothing) instead of string '!test!!' (matching nothing - 'test' - nothing) or string 'test!' (matching 'test') it's at least counter-intuitive