PHP将字符串首字母大小写转换的实例

六月 13, 2019 | views
Comments 0

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

  1. <?php 
  2. $foo='hello world!'
  3. $foo= ucwords($foo);      // Hello World!  
  4. $bar='HELLO WORLD!'
  5. $bar= ucwords($bar);      // HELLO WORLD! 
  6. $bar= ucwords(strtolower($bar));// Hello World! 
  7. ?> 

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

  1. <?php 
  2. $foo='hello world!'
  3. $foo= ucfirst($foo);      // Hello world! 
  4. /phpfensi.com  
  5. $bar='HELLO WORLD!'
  6. $bar= ucfirst($bar);      // HELLO WORLD! 
  7. $bar= ucfirst(strtolower($bar));// Hello world! 
  8. ?> 

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

  1. <?php 
  2.  $foo='HelloWorld'
  3.  $foo= lcfirst($foo);      // helloWorld 
  4.   
  5.  $bar='HELLO WORLD!'
  6.  $bar= lcfirst($bar);      // hELLO WORLD! 
  7.  $bar= lcfirst(strtoupper($bar));// hELLO WORLD! 
  8.  ?> 

所有 字母变大写:strtoupper()

所有 字母变小写:strtolower()



zend