使用ucfirst使每个句子的首字母大写

Making the first letter of every sentence upper case using ucfirst

我试图让每个句子的首字母大写,同时保留标点符号。我试过 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?!"

试试这个:

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