How can I replace the second character in a string with another character?
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);
Last edited by james438; 02-25-2008 at 05:20 AM. Reason: formatting
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.
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
james438 (02-26-2008)
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] = ' ';
james438 (02-26-2008)
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.![]()
Thou com'st in such a questionable shape
Hamlet, Act 1, Scene 4
Bookmarks