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

$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);

Leave a Comment