PHP 大小寫轉換函數 lcfirst ucfirst ucwords strtolower strtoupper
- lcfirst – 將字符串的首字母轉換爲小寫 (PHP 5 >= 5.3.0, PHP 7)
- ucfirst – 將字符串的首字母轉換爲大寫 (PHP 4, PHP 5, PHP 7)
- ucwords – 將字符串中每個單詞的首字母轉換爲大寫 (PHP 4, PHP 5, PHP 7)
- strtolower – 將字符串轉化爲小寫 (PHP 4, PHP 5, PHP 7)
- strtoupper – 將字符串轉化爲大寫 (PHP 4, PHP 5, PHP 7)
lcfirst
將字符串的首字母轉換爲小寫
string lcfirst ( string $str )
參數說明
str
輸入的字符串。
返回值
返回轉換後的字符串。
注意
如果str的第一個字符是字母,則將其轉換爲小寫。需要注意的是“字母”是由當前語言區域決定的。
示例
<?php
$foo = 'HelloWorld';
$foo = lcfirst($foo);
var_dump($foo); //string 'helloWorld' (length=10)
$foo = 'helloWorld';
$foo = lcfirst($foo);
var_dump($foo); //string 'helloWorld' (length=10)
$foo = '中文';
$foo = lcfirst($foo);
var_dump($foo); //string '中文' (length=4)
?>
ucfirst
將字符串的首字母轉換爲大寫
string ucfirst ( string $str )
參數說明
str
輸入的字符串。
返回值
返回轉換後的字符串。。
注意
如果str的第一個字符是字母,則將其轉換爲大寫。需要注意的是“字母”是由當前語言區域決定的。
示例
<?php
$foo = 'hello world!';
$foo = ucfirst($foo);
var_dump($foo); //string 'Hello world!' (length=12)
$foo = 'HELLO WORLD!';
$foo = ucfirst($foo);
var_dump($foo); //string 'HELLO WORLD!' (length=12)
$foo = '中文';
$foo = ucfirst($foo);
var_dump($foo); //string '中文' (length=4)
?>
ucwords
將字符串中每個單詞的首字母轉換爲大寫
string ucwords ( string $str )
參數說明
str
輸入的字符串
返回值
返回轉換後的字符串。
注意
將 str 中每個單詞的首字符(如果首字符是字母)轉換爲大寫字母,並返回這個字符串。
這裏單詞的定義是緊跟在空白字符(空格符、製表符、換行符、回車符、水平線以及豎線)之後的子字符串。
示例
<?php
$foo = 'hello world!';
$foo = ucwords($foo);
var_dump($foo); //string 'Hello World!' (length=12)
$foo = 'HELLO WORLD!';
$foo = ucwords($foo);
var_dump($foo); //string 'HELLO WORLD!' (length=12)
$foo = '中文!';
$foo = ucwords($foo);
var_dump($foo); //string '中文!' (length=5)
?>
strtolower
將字符串轉化爲小寫
string strtolower ( string $string )
參數說明
string
輸入的字符串
返回值
返回轉換後的字符串。
注意
將 string 中所有的字母字符轉換爲小寫並返回。
注意 “字母” 與當前所在區域有關。
示例
<?php
$foo = "How Are You!";
$foo = strtolower($foo);
var_dump($foo); //string 'how are you!' (length=12)
$foo = "中文!";
$foo = strtolower($foo);
var_dump($foo); //string '中文!' (length=5)
//實現 lcfirst()
$foo = "How Are You!";
$foo{0} = strtolower($foo{0});
var_dump($foo); //'how Are You!' (length=12)
?>
strtoupper
將字符串轉化爲大寫
string strtoupper ( string $string )
參數說明
string
輸入的字符串
返回值
返回轉換後的字符串。
注意
將 string 中所有的字母字符轉換爲大寫並返回。
注意 “字母” 與當前所在區域有關。
示例
<?php
$foo = "How are you!";
$foo = strtoupper($foo);
var_dump($foo); //string 'HOW ARE YOU!' (length=12)
$foo = "中文!";
$foo = strtoupper($foo);
var_dump($foo); //string '中文!' (length=5)
//實現 ucfirst()
$foo = "how are you!";
$foo{0} = strtoupper($foo{0});
var_dump($foo); //string 'How are you!' (length=12)
?>