PHP str_replace numbers with other numbers

php, string

Solution

That produces the output `0123443210` because str_replace with an array will start replacing earlier matches as it iterates over `$numbers`

For a single letter transposition like this, use strtr instead

 $encoded = strtr($pre, "0123456789", "9876543210");

Problem

My string of numbers are not replacing correctly. I am expecting the output after replacing to be `9876543210` but it doesnt seem to be the case. What am i doing wrong? ``` <?php $numbers = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); $code = array('9', '8', '7', '6', '5', '4', '3', '2', '1', '0'); $pre = '0123456789'; echo $pre . " ==> " . str_replace($numbers, $code, $pre); ?> ```

Original source