简体   繁体   English

将字符串分成两个单词后,将两个字母大写

[英]Capitalize two letters after splitting the string into two words

I have a string: 我有一个字符串:

$name = ucfirst('kasiang');

I want to split it into two words and create Ka Siang 我想将其分为两个词并创建Ka Siang

So what I tried: 所以我尝试了:

echo substr_replace($name,' '.substr($name,2),2)

But i am getting only Ka siang . 但是我只得到Ka siang Is there any way to capitalize second letter too? 还有什么办法可以大写第二个字母吗?

You can do like below. 您可以像下面这样。

<?php 

$name = ucfirst('kasiang');
echo substr_replace($name,' '.ucfirst(substr($name,2)),2);

?>

Another way is: 另一种方法是:

<?php 
echo ucwords(substr_replace($name,' '.substr($name,2),2));
?>

You can do it with ucwords , like comment above says: 您可以使用ucwords来做到,就像上面的评论说的那样:

<?php
$name = ucfirst('kasiang');

echo ucwords(substr_replace($name,' '.substr($name,2),2));

Result: 结果:

Ka Siang

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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