|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-02-20 14:30 UTC] root at microsoft dot com
Description:
------------
Behavior of preg_replace is non-obvious, if second argument is function call.
Test script:
---------------
class My{
function prepare($text){
return preg_replace(
'/\{(.+?)\}/',
$this->rand(explode('|', '\\1')),
$text
);
}
private function rand(array $values){
return $values[rand(0, sizeof($values)-1)];
}
}
echo (new My)->prepare('i choose {ps3|games}');
Expected result:
----------------
i choose ps
or
i choose games
Actual result:
--------------
i choose ps3|games
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 23:00:01 2025 UTC |
return preg_replace('/\{(.+?)\}/', $this->rand(explode('|', '\\1')), $text); is same as: return preg_replace('/\{(.+?)\}/', '\1', $text); for $this->rand(explode('|', '\\1')) will be called first and the result as the second argument to preg_replace. so I see no problem here..