1. 程式人生 > >PHP英文字母大小寫轉換函數小結

PHP英文字母大小寫轉換函數小結

字符串 content 字符轉換 class 文字 單詞 con 英文 code

每個單詞的首字母轉換為大寫:ucwords()

代碼如下: <?php
$foo = ‘hello world!‘;
$foo = ucwords($foo); // Hello World!

$bar = ‘HELLO WORLD!‘;
$bar = ucwords($bar); // HELLO WORLD!
$bar = ucwords(strtolower($bar)); // Hello World!
?>

第一個單詞首字母變大寫:ucfirst()

代碼如下: <?php
$foo = ‘hello world!‘;
$foo = ucfirst($foo); // Hello world!

$bar = ‘HELLO WORLD!‘;
$bar = ucfirst($bar); // HELLO WORLD!
$bar = ucfirst(strtolower($bar)); // Hello world!
?>

第一個單詞首字母變小寫:lcfirst()

代碼如下: <?php
$foo = ‘HelloWorld‘;
$foo = lcfirst($foo); // helloWorld

$bar = ‘HELLO WORLD!‘;
$bar = lcfirst($bar); // hELLO WORLD!
$bar = lcfirst(strtoupper($bar)); // hELLO WORLD!
?>

所有字母變大寫:strtoupper()
所有字母變小寫:strtolower()

  • 1 strtolower() //將字符串轉換為小寫形式
  • 2 strtoupper() //將字符串轉換為大寫形式
  • 3 ucfirst() //將字符串的第一個字符轉換為大寫形式
  • 4 ucwords() //將字符串中每一個單詞的首字母轉換為大寫形式

PHP英文字母大小寫轉換函數小結