如何在字符串的第一个字符后添加空格


How to add a space after fist character of string

我有一个名为$words = "Unbelievable的字符串。我希望输出为U nbelievable

我试过这个代码:

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

它正在工作,但它影响string中的每2个字符。

我用上面的代码得到了这个Un be li ev ab le

我只想在string 的第一个字符后添加一个空格

您可以使用substr()或substra_replace()来实现此目的。尝试使用任一

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

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

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