$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
您可以使用字符串append和substr来做到这一点
$var = $var[0].":".substr($var,1);
$str = substr($str, 0, 1) . ':' . substr($str, 1)
$newstr = substr_replace($oldstr, $str_to_insert, $pos, 0);
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");
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.