萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php中英文字符轉換大小字代碼

php中英文字符轉換大小字代碼

在php中會為我們提供一些常用的字符操作函數,下面我來整理了php中字符大小字轉換的函數,這裡我會一個個介紹,希望例子能給各位同學帶來幫助。

每個單詞的首字母轉換為大寫: ucwords()
第一個單詞首字母變大寫: ucfirst()
第一個單詞首字母變小寫: lcfirst()
所有 字母變大寫: strtoupper()

例子

 代碼如下 復制代碼

<?php
echo strtoupper("Hello WORLD!");
?>

輸出:

HELLO WORLD!

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

 代碼如下 復制代碼

<?php
$foo = 'where are doing?!';
$foo = ucfirst($foo);             // where are doing?!

$bar = 'where are doing?!';
$bar = ucfirst($bar);             // where are doing?!
$bar = ucfirst(strtolower($bar)); // where are doing?!
?>

所有 字母變小寫: strtolower()

 代碼如下 復制代碼

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


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

 代碼如下 復制代碼

<?php
$foo = 'where are doing?!';
$foo = ucwords($foo);             // Where are doing?!

$bar = 'where are doing?!';
$bar = ucwords($bar);             // Where are doing?!
$bar = ucwords(strtolower($bar)); // Where are doing?!
?>

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

 代碼如下 復制代碼

<?php
$foo = 'HelloWorld';
$foo = lcfirst($foo);             // where are doing

$bar = 'where are doing?!';
$bar = lcfirst($bar);             // where are doing?!
$bar = lcfirst(strtoupper($bar)); // where are doing?!
?>

copyright © 萬盛學電腦網 all rights reserved