将所有大写的输入更改为正常情况

Tra*_*hor 2 php

我想将php中的字符串从所有大写更改为正常情况.因此,每个句子都以大写字母开头,其余句子都是小写字母.

有一个简单的方法吗?

Pau*_*xon 10

一种简单的方法是使用strtolower使字符串小写,而ucfirst大写第一个字符如下:

$str=ucfirst(strtolower($str));
Run Code Online (Sandbox Code Playgroud)

如果字符串包含多个句子,则必须编写自己的算法,例如在句子分隔符上爆炸并依次处理每个句子.除了第一个字符,您可能需要一些启发式方法来处理"I"之类的单词以及出现在文本中的任何常用专有名词.例如,像这样:

$sentences=explode('.', strtolower($str));
$str="";
$sep="";
foreach ($sentences as $sentence)
{
   //upper case first char
   $sentence=ucfirst(trim($sentence));

   //now we do more heuristics, like turn i and i'm into I and I'm
   $sentence=preg_replace('/i([\s\'])/', 'I$1', $sentence);

   //append sentence to output
   $str=$sep.$str;
   $sep=". ";
}
Run Code Online (Sandbox Code Playgroud)