How can I replace the second character in a string with another character?
Printable View
How can I replace the second character in a string with another character?
sorry dumb question.
Code:$string="blah"
$string=substr_replace($string,"",1,1);
Though you found a better solution, you can always figure out something like this just using substr():
$a = $a[0].'X'.substr($a,2);
In fact, you might be able to just use: $a[1]='X';, but I'm not sure if that will cause any trouble with the variable type.
You can use brackets as if it were an array. After all, a string is really an array of characters with numerical indices.
This replaces the 2nd letter of the variable "string" with a space.PHP Code:$string[1] = ' ';
those are some really interesting alternatives. I still plan on using the built in php function substr_replace(), but I really like the work around solutions both of you have posted.
And here we find yet another beautiful thing about PHP: there's always so many ways to do things. :)