【问题标题】:Making the first letter of every sentence upper case using ucfirst使用 ucfirst 使每个句子的第一个字母大写
【发布时间】:2015-08-28 02:16:47
【问题描述】:

我正在尝试使每个句子的第一个字母大写,同时保留标点符号。我试过 ucfirst,但它只使字符串的第一个字母大写,而不是所有其他句子。我该如何解决这个问题?

$text = "yes. are you listening to me? huh?!"
$text = ucfirst($text);

echo $text;

预期输出:

Yes. Are you listening to me? Huh?!"

实际输出:

Yes. are you listening to me? huh?!"

【问题讨论】:

标签: php string uppercase ucfirst


【解决方案1】:

试试这个:

function ucfirstSentence($str){
     $str = ucfirst(strtolower($str));
     $str = preg_replace_callback('/([.!?])\s*(\w)/', 
       create_function('$matches', 'return strtoupper($matches[0]);'), $str);
     return $str;
}

【讨论】:

    猜你喜欢
    • 2018-03-06
    • 2014-05-13
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多