如何使用PHP将段落中单词的每个第一个字母变成大写字母

发布于 2024-10-18 16:02:39 字数 232 浏览 2 评论 0原文

我有一个段落,我想使用 PHP 将单词的每个第一个字母变成大写字母。

例如:

converting every first letter into capital letter.

应该转换成

Converting Every First Letter Into Capital Letter.

谢谢

I am having a paragraph and i want to make every first letter of the word into a capital letter using PHP.

ex:

converting every first letter into capital letter.

should convert into

Converting Every First Letter Into Capital Letter.

Thank You

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

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

发布评论

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

评论(4

我们的影子 2024-10-25 16:02:39

如果这只是为了演示,您还可以使用CSS:

  • 大写:将每个单词中的第一个字符转换为大写;所有其他字符不受影响 - 它们不会转换为小写,但会按文档中的写入方式显示。

示例:

p {
    text-transform: capitalize
}

请参阅

If this is strictly for presentation, you can also use CSS for this:

  • capitalize: transforms the first character in each word to uppercase; all other characters remain unaffected — they’re not transformed to lowercase, but will appear as written in the document.

Example:

p {
    text-transform: capitalize
}

See

尸血腥色 2024-10-25 16:02:39

有一个函数可以实现这一点 - ucwords()

echo ucwords("converting every first letter into capital letter.");

There's a function for that — ucwords():

echo ucwords("converting every first letter into capital letter.");
沩ん囻菔务 2024-10-25 16:02:39

要转换每个第一个字母大写,请使用以下代码

Ex)

<?php
$data = "converting every first letter into capital letter.";
echo ucwords($data);
?>

输出:
将每个第一个字母转换为大写字母。

要转换句子中的第一个字母大写,请使用以下代码
例如)

<?php
$data = "converting every first letter into capital letter.";
echo ucfirst($data);
?>

输出:
将每个第一个字母转换为大写字母。

要转换所有句子中的第一个字母大写,请使用以下代码。
例如)

<?php
$string = "this is a first message. this is a second message. this is a third message! hope this helps.";

$string = strtolower($string);
echo preg_replace('/(^|[\.!?]"?\s+)([a-z])/e', '"$1" . ucfirst("$2")', $string); 
?>

输出:
这是第一条消息。这是第二条消息。这是第三条消息!希望这有帮助。

For converting every first letter capital use the below code

Ex)

<?php
$data = "converting every first letter into capital letter.";
echo ucwords($data);
?>

Output:
Converting Every First Letter Into Capital Letter.

For converting the first letter capital in a sentence use the below code
Ex)

<?php
$data = "converting every first letter into capital letter.";
echo ucfirst($data);
?>

Output:
Converting every first letter into capital letter.

For converting the first letter capital in all sentence use the below code.
Ex)

<?php
$string = "this is a first message. this is a second message. this is a third message! hope this helps.";

$string = strtolower($string);
echo preg_replace('/(^|[\.!?]"?\s+)([a-z])/e', '"$1" . ucfirst("$2")', $string); 
?>

Output:
This is a first message. This is a seconde message. This is a third message! Hope this helps.

不乱于心 2024-10-25 16:02:39

Nithin Raja,你的榜样很棒!
下面的代码调整为>= PHP7
谢谢!

$string = "Olá, tudo bem? Esta é a minha segunda frase.\nAqui já estou na terceira. E agora encerrando o texto.";
$capitalize = preg_replace_callback('/(^|[\.!?]"?\s+)([a-z])/', function($parte){return $parte[1] . ucfirst($parte[2]);}, $string);
echo nl2br($capitalize);

Nithin Raja, your example is great!
Below the code adjusted for >= PHP7
Thanks!

$string = "Olá, tudo bem? Esta é a minha segunda frase.\nAqui já estou na terceira. E agora encerrando o texto.";
$capitalize = preg_replace_callback('/(^|[\.!?]"?\s+)([a-z])/', function($parte){return $parte[1] . ucfirst($parte[2]);}, $string);
echo nl2br($capitalize);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文