简体   繁体   中英

How to add a space after fist character of string

I have a string named $words = "Unbelievable . I want the output to be U nbelievable

I tried this code:

implode(" ", str_split($words, 2))." ";

It's working but it affects on every 2 characters in the string .

I got this Un be li ev ab le using the code above.

I want only to add a space after first character in a string

You can use substr() or substr_replace() for this purpose. Try with either

$words = "Unbelievable";
echo substr($words, 0, 1) . ' ' . substr($words, 1);

or

echo substr_replace($words," ", 1, -strlen($words));

Use substr()

$words = "Unbelievable";
$str1 = substr($words, 0, 1); //set str1 to be Un
$str2 = substr($words, 1); //set str2 to be believable

$result = $str1." ".$str2; //result is 'Un believable'
 $words = "Unbelievable".    
$word = substr_replace($words," ", 1, -strlen($words));

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM