0

可能重复:
如何将第一个字母显示为大写?
PHP将句子中第一个单词的首字母大写

我想大写句子中的第一个字母和句号。谁能建议怎么做?

例如,

//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.
4

2 回答 2

2

下面试试。

它将运行该函数以大写具有多个句子的字符串中的句号(句点)之后的每个字母。

    $string = ucfirst(strtolower($string));     

    $string = preg_replace_callback('/[.!?].*?\w/', create_function('$matches', 'return strtoupper($matches[0]);'),$string);

    echo $string;

请进行必要的更改。

于 2012-01-21T07:43:07.433 回答
0

尝试这个:

<?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;

?>
于 2012-01-21T08:23:54.483 回答