Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中如何将句子中的第一个字母大写?_Php_Text Segmentation - Fatal编程技术网

在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],但这两种方法都有很大的错误空间。