如何在PHP中将句子中的第一个字母大写?

发布于 2024-12-28 05:29:00 字数 1115 浏览 1 评论 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.

Possible Duplicate:
How do I display the first letter as uppercase?
PHP capitalize first letter of first word in a sentence

I want to uppercase the first letter in a sentence and after a period. Can anyone suggest how to do?

For example,

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

I want the following output.

Needs to identify areas of strength and 
weakness. Sets goals for self-improvement.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄意 2025-01-04 05:29:00

尝试下面。

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

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

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

    echo $string;

请进行必要的更改。

Try below.

It will run the function to capitalize every letter AFTER a full-stop (period) in a string having multiple sentences.

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

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

    echo $string;

Please do required changes.

紅太極 2025-01-04 05:29:00

试试这个:

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

?>

Try this:

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

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文