douchi0471 2015-11-29 13:58
浏览 36
已采纳

str_replace函数如何工作?

I'm trying to understand the str_replace function.

Code:

$a = array(1,8,7,5);
$b = array(3,7,11,6);
$str = '879';
$c = str_replace($a, $b , $str);
echo $c;

Output:

11119

I don't understand the output. Can someone explain how the str_replace function works?

  • 写回答

2条回答 默认 最新

  • doumo1807831 2015-11-29 14:04
    关注

    str_replace replaces pair of values from provided arrays $a, $b. And does it in order, so str_replace($a, $b , $str) means:

    replace 1 to 3,
    then replace 8 to 7,
    then replace 7 to 11
    and finally replace 5 to 6.
    

    So, let's go:

    • input 879, replace 1 to 3, output 879
    • input 879, replace 8 to 7, output 779
    • input 779, replace 7 to 11, output 11119
    • input 11119, replace 5 to 6, output 11119
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?