如何在PHP中将句子中的第一个字母大写? [英] How to uppercase the first letter in a sentence in PHP?

查看:135
本文介绍了如何在PHP中将句子中的第一个字母大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

例如

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

推荐答案

尝试以下方法.

在包含多个句子的字符串中,句号(句点)结束后,它将运行该功能以大写每个字母.

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;

请进行必要的更改.

这篇关于如何在PHP中将句子中的第一个字母大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆