I was working a bit with preg_replace
over the weekend and I was reading the Php preg_replace
documentation when I saw something odd.
Example #2 from the docs shows that when given the following php code
<?php
$string = 'The quick brown fox jumped over the lazy dog.';
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);
?>
the output will be
"The bear black slow jumped over the lazy dog."
and in order to generate what (in my opinion) should be output by default I would need to call ksort()
beforehand. like this:
<?php
ksort($patterns);
ksort($replacements);
echo preg_replace($patterns, $replacements, $string);
?>
Isn't this really a work-around for a bug in php's preg_replace()
? Why does php behave this way? Is there some idiosyncrasy with the arrays declared here that I am missing?