$var = "Hello";
How can I insert the character : in after the first character of the string above ? The first character could be anything. So I want to do something like
$res = add("Hello");
echo $res; // which return H:ello
I know how to do this through str_replace
but this needs the first character to be always the same..
Any help would be highly appreciated
you can use string append and substr to do it
$var = $var[0].":".substr($var,1);
$newstr = substr_replace($oldstr, $str_to_insert, $pos, 0);
$str = substr($str, 0, 1) . ':' . substr($str, 1)
In PHP I would probably try this:
$string = "Hello";
$new_string = substr($string,0,1) . ":" . substr($string,1,$strlen($string)-1);
This would get H (= first char) + : (= fixed char) + ello (= the rest of the string)
<?php
function addColon($myString){
return $myString = substr($myString, 0, 1) . ':' . substr($myString, 1);
}
echo addColon("Hello");
?>