加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 将句子中的第一个单词更改为大写

发布时间:2020-12-13 17:16:03 所属栏目:PHP教程 来源:网络整理
导读:我希望将句子的第一个单词转换为大写但不确定我将如何处理它.这是我到目前为止: $o = ""; if((preg_match("/ibm/i",$m)) || (preg_match("/hpspecial/i",$m))) {$o = strtoupper($m);} else { $o = ucwords(strtolower($m));} 我想使用hpspecial ID中的大写
我希望将句子的第一个单词转换为大写但不确定我将如何处理它.这是我到目前为止:

$o = "";

  if((preg_match("/ibm/i",$m)) || (preg_match("/hpspecial/i",$m))) {

$o = strtoupper($m);

} else {

  $o = ucwords(strtolower($m));

}

我想使用hpspecial ID中的大写字母,其中HP是要大写的句子的第一部分,但这将使整个句子大写.我如何才使HP的句子大写?

解决方法

你可以点到 ucfirst

ucfirst — Make a string’s first character uppercase

喜欢

$foo = 'hello form world';
$foo = ucfirst($foo);

live output

编辑:使它成为第一个单词大写你可以做

<?php
$foo = 'hello form world ';

$pieces = explode(" ",$foo);
$pieces[0] = strtoupper($pieces[0]);
$imp = implode(" ",$pieces);
echo $imp;

live result

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读