在PHP中如何将句子中的第一个字母大写?
可能重复:在PHP中如何将句子中的第一个字母大写?,php,text-segmentation,Php,Text Segmentation,可能重复: 我想把句子中的第一个字母大写,并在句点之后。有人能建议怎么做吗 比如说, //I have the following in a language class. "%s needs to identify areas of strength and weakness. %s sets goals for self-improvement."; // in a view $contone=$this->lang->line($colstr);// e.g get
我想把句子中的第一个字母大写,并在句点之后。有人能建议怎么做吗 比如说,
//I have the following in a language class.
"%s needs to identify areas of strength and
weakness. %s sets goals for self-improvement.";
// in a view
$contone=$this->lang->line($colstr);// e.g get the above string.
//$conttwo=substr($contone, 3);//skip "%s " but this doesnot work when there
//are more than one %s
$conttwo=str_replace("%s ", "", $contone);// replace %s to none
$contthree = ucfirst($conttwo); // this only uppercase the first one
我想要以下输出
Needs to identify areas of strength and
weakness. Sets goals for self-improvement.
试试下面
它将运行函数,在包含多个句子的字符串中,在句号(句点)后将每个字母大写
$string = ucfirst(strtolower($string));
$string = preg_replace_callback('/[.!?].*?\w/', create_function('$matches', 'return strtoupper($matches[0]);'),$string);
echo $string;
请执行所需的更改。尝试以下操作:
<?php
//define string
$string = "your sentences";
//first we make everything lowercase, and then make the first letter if the entire string capitalized
$string = ucfirst(strtolower($string));
//now we run the function to capitalize every letter AFTER a full-stop (period).
$string = preg_replace_callback('/[.!?].*?\w/', create_function('$matches', 'return strtoupper($matches[0]);'),$string);
//print the result
echo $string;
?>
可靠吗?可能不可能。很难区分句号和小数点。你可以把绳子在绳子上炸开。字符,ucfirst数组中的每个元素并内爆()结果,或preg_replace for。字符后跟至少一个空格[a-z],但这两种方法都有很大的错误空间。