php中英文字符转换大小字代码

作者:袖梨 2022-06-24

每个单词的首字母转换为大写: ucwords()
第一个单词首字母变大写: ucfirst()
第一个单词首字母变小写: lcfirst()
所有 字母变大写: strtoupper()

例子

 代码如下 复制代码

echo strtoupper("Hello WORLD!");
?>

输出:

HELLO WORLD!

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

 代码如下 复制代码

$foo = 'where are doing?!';
$foo = ucfirst($foo);             // where are doing?!

$bar = 'where are doing?!';
$bar = ucfirst($bar);             // where are doing?!
$bar = ucfirst(strtolower($bar)); // where are doing?!
?>

所有 字母变小写: strtolower()

 代码如下 复制代码

$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtolower($str);
echo $str; // Prints mary had a little lamb and she loved it so
?>


每个单词的首字母转换为大写:ucwords()

 代码如下 复制代码

$foo = 'where are doing?!';
$foo = ucwords($foo);             // Where are doing?!

$bar = 'where are doing?!';
$bar = ucwords($bar);             // Where are doing?!
$bar = ucwords(strtolower($bar)); // Where are doing?!
?>

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

 代码如下 复制代码

$foo = 'HelloWorld';
$foo = lcfirst($foo);             // where are doing

$bar = 'where are doing?!';
$bar = lcfirst($bar);             // where are doing?!
$bar = lcfirst(strtoupper($bar)); // where are doing?!
?>

相关文章

精彩推荐