Jairus Holein
Jairus Holein

Reputation: 63

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

Upvotes: 2

Views: 2211

Answers (3)

Jenz
Jenz

Reputation: 8369

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));

Upvotes: 5

Mad Angle
Mad Angle

Reputation: 2330

 $words = "Unbelievable".    
$word = substr_replace($words," ", 1, -strlen($words));

Upvotes: 0

Parker
Parker

Reputation: 8851

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'

Upvotes: 1

Related Questions