function replace_char($string, $position, $newchar) {
if(strlen($string) <= $position) {
return $string;
}
$string[$position] = $newchar;
return $string;
}
It's safe to treat strings as arrays in PHP, as long as you don't try to change chars after the end of the string. See the manual on strings: