I have a $string and want to loop through inserting characters inbetween the letters. What's the best function to do this in php? I apologise if I'm being a muppet.
There's loads of ways to do this; keeping it simple, something like the following; $string = "This is my string"; $newstring = ""; for($x=0; $x<strlen($string);$x++) { $newstring .= $string[$x]; if($x != strlen($string)-1) $newstring .= "CHAR TO INSERT"; } PHP: If you have more specific requirements, let me know
You could also do something like: $string = preg_replace("/([a-z])\B/i", "\$1$insert", $string); PHP: You can run this function here: http://www.exorithm.com/algorithm/view/insert_into_words
function stringrpl($x,$r,$str) { $out = ""; $temp = substr($str,$x); $out = substr_replace($str,"$r",$x); $out .= $temp; return $out; } $test = stringrpl(2,"c",$test); echo $test;