首页 > php代码 > php字符串首字母转换大小写实例

php字符串首字母转换大小写实例

在php中如果我们要把字符转换成大小写直接使用strtolower或者strtoupper即可,但是要首字母要转换大小写我们需要使用ucwords相关函数

首字母变大写:ucwords()

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

第一个词首字母变大写:ucfirst()

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

第一个词首字母小写lcfirst()

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


教程地址:http://www.phprm.com/code/54761.html

欢迎转载!但请带上文章地址^^

标签:none

发表留言