Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我试图使每个句子的第一个字母大写,同时保留标点符号。我尝试过 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; }