php中英文字符转换大小写代码
在php中会为我们提供一些常用的字符操作函数,下面我来整理了php中字符大小写转换的函数,这里我会一个个介绍,希望例子能给各位同学带来帮助.
每个单词的首字母转换为大写:ucwords()
第一个单词首字母变大写:ucfirst()
第一个单词首字母变小写:lcfirst()
所有 字母变大写:strtoupper()
PHP实例代码如下:
- <?php
- echo strtoupper("Hello WORLD!");
- ?>
- //输出:HELLO WORLD!
第一个单词首字母变大写:ucfirst(),代码如下:
- <?php
- $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(),代码如下:
- <?php
- $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(),代码如下:
- <?php
- $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(),代码如下:
- <?php
- $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?!
- ?>