使每个句子大写的第一个字符

Making the first character of every sentence capital case

我有一个函数可以让每个新句子的字符都变成大写,但是,它不能正常工作。它仅在新单词紧靠标点符号时有效,而在标点符号后有 space 时无效。我该如何解决这个问题?

//****************************************************************
function ucAll($str) {

return preg_replace_callback('/(?<=^|[\.\?!])[^\.]/', function ($match) {
return strtoupper($match[0]);
}, $str);

} //end of function ucAll($str)
//****************************************************************

$string = "i dont' want to? why should i?";
$string = ucAll($string);
echo $string;

结果

我不想?我为什么要?

需要结果

我不想?我为什么要?

只需在正则表达式的适当位置添加 (\s)*

<?php

//****************************************************************
function ucAll($str) {

    return preg_replace_callback('/(?<=^|[\.\?!])(\s)*[^\.]/', function ($match) {
        return strtoupper($match[0]);
    }, $str);

} //end of function ucAll($str)
//****************************************************************

$string = "i dont' want to? why should i?";
$string = ucAll($string);
echo $string;