str_replace with array

前端 未结 5 1246
粉色の甜心
粉色の甜心 2020-12-01 23:33

I\'m having some troubles with the PHP function str_replace when using arrays.

I have this message:

$message = strtolower(\"L rzzo rwldd         


        
5条回答
  •  萌比男神i
    2020-12-02 00:05

    Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements.

        // Outputs F because A is replaced with B, then B is replaced with C, and so on...
        // Finally E is replaced with F, because of left to right replacements.
        $search  = array('A', 'B', 'C', 'D', 'E');
        $replace = array('B', 'C', 'D', 'E', 'F');
        $subject = 'A';
        echo str_replace($search, $replace, $subject);
    

提交回复
热议问题