PHP str_replace

前端 未结 7 2011
渐次进展
渐次进展 2021-01-25 11:03

I have the string $var in which I need to replace some text. The first \"X\" needs to be replaced by \"A\", the second \"X\" needs to be replaced by B and so on, here is an exam

7条回答
  •  执笔经年
    2021-01-25 11:38

    Lots of loops are happening in the other answers, here's an alternative.

    $var = 'X X X X X X X';
    $replace = array('A', 'B', 'C', 'D');
    $var = preg_replace(array_fill(0, count($replace), '/X/'), $replace, $var, 1);
    echo $var; // A B C D X X X
    

提交回复
热议问题