Trying to replace parts of string start with same search chars

后端 未结 3 2064
北海茫月
北海茫月 2020-12-02 03:19

I\'m trying to replace parts of my string. But I met a problem when my search string start with same character:

$string = \"Good one :y. Keep going :y2\"; 

         


        
3条回答
  •  孤街浪徒
    2020-12-02 03:54

    Try to replace first for :y2 and then for :y

    $string = "Good one :y. Keep going :y2"; 
    
    $my_array= array(":y2" => "b", ":y" => "a");
    
    $str = str_replace(array_keys($my_array), array_values($my_array), $string);
    

    outputs

    Good one a. Keep going b
    

    Try it

提交回复
热议问题