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

在php中会为我们提供一些常用的字符操作函数,下面我来整理了php中字符大小写转换的函数,这里我会一个个介绍,希望例子能给各位同学带来帮助.

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

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

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

所有 字母变大写:strtoupper()

PHP实例代码如下:

  1. <?php
  2. echo strtoupper("Hello WORLD!");
  3. ?>
  4. //输出:HELLO WORLD!

第一个单词首字母变大写:ucfirst(),代码如下:

  1. <?php
  2. $foo = 'where are doing?!';
  3. $foo = ucfirst($foo); // where are doing?!
  4. $bar = 'where are doing?!';
  5. $bar = ucfirst($bar); // where are doing?!
  6. $bar = ucfirst(strtolower($bar)); // where are doing?!
  7. ?>

所有 字母变小写:strtolower(),代码如下:

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

每个单词的首字母转换为大写:ucwords(),代码如下:

  1. <?php
  2. $foo = 'where are doing?!';
  3. $foo = ucwords($foo); // Where are doing?!
  4. $bar = 'where are doing?!';
  5. $bar = ucwords($bar); // Where are doing?!
  6. $bar = ucwords(strtolower($bar)); // Where are doing?!
  7. ?>

第一个单词首字母变小写:lcfirst(),代码如下:

  1. <?php
  2. $foo = 'HelloWorld';
  3. $foo = lcfirst($foo); // where are doing
  4. $bar = 'where are doing?!';
  5. $bar = lcfirst($bar); // where are doing?!
  6. $bar = lcfirst(strtoupper($bar)); // where are doing?!
  7. ?>