PHP function to replace a (i)th-position character

前端 未结 5 1715
梦如初夏
梦如初夏 2020-12-03 10:20

Is there a function in PHP that takes in a string, a number (i), and a character (x), then replaces the character at position (i) with

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 10:44

    $str    = 'bar';
    $str[1] = 'A';
    echo $str; // prints bAr
    

    or you could use the library function substr_replace as:

    $str = substr_replace($str,$char,$pos,1);
    

提交回复
热议问题