首页 > 开发 > PHP > 正文

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

2024-05-04 22:00:52
字体:
来源:转载
供稿:网友

每个单词的首字母转换为大写: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. /Vevb.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()

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表