PHP将字符串首字母大小写转换【php】

原创
2020/10/10 18:07
阅读数 297

搞了PHP半生,仍然是个小兵

不能似懂非懂,回看细节等等

一、每个单词的首字母转化为大写

<?php
$foo = 'hello world!';
$foo = ucwords($foo);             // Hello World!
 
$bar = 'HELLO WORLD!';
$bar = ucwords($bar);             // HELLO WORLD!
$bar = ucwords(strtolower($bar)); // Hello World!
?>

二、第一个单词的首字母变大写

<?php
$foo = 'hello world!';
$foo = ucfirst($foo);             // Hello world!
 
$bar = 'HELLO WORLD!';
$bar = ucfirst($bar);             // HELLO WORLD!
$bar = ucfirst(strtolower($bar)); // Hello world!
?>

三、第一个单词的首字母变小写

<?php
$foo = 'HelloWorld';
$foo = lcfirst($foo);             // helloWorld
 
$bar = 'HELLO WORLD!';
$bar = lcfirst($bar);             // hELLO WORLD!
$bar = lcfirst(strtoupper($bar)); // hELLO WORLD!
?>

四、拓展

所有 字母变大写:strtoupper()

所有 字母变小写:strtolower()

展开阅读全文
php
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部