Say I hate the word "hat". I want a function that would work something like strreplace("hat", 1, "o") therefore changing it into hot instead of hat.
Is there a way to do this with a function? Or do I have to write my own?
function changeChar($string,$newchar,$pos){
$string[$pos] = $newchar;
return $string;
}
echo changeChar("Logs","L",2);
Would echo "LoLs"
Logs
0123 <position (g is the 2nd character ;)
insert this just below the first line if you want the first character to be the 1st not 0th:
$pos = $pos + 1;
All you need is str_replace(). See here:http://www.php.net/manual/en/function.str-replace.php
e.g.
echo str_replace("a", "o", "hat");
// outputs 'hot'
$foo = 'hat'; $foo[1] = 'o';