首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 使用 strtolower() 與 strtoupper() 轉換字串大小寫

     編輯:PHP 綜合知識

strtolower() 將字串轉為全小寫,strtoupper() 轉為全大寫。僅影響 ASCII 英文字母,其他字元保持不變。

基本範例

<?php
$string = "Learn PHP string functions at jb51.net";
$lower = strtolower($string);
$upper = strtoupper($string);
echo "$lower\n"; // learn php string functions at jb51.net
echo "$upper\n"; // LEARN PHP STRING FUNCTIONS AT JB51.NET
?>

多位元組字串請用 mb_strtolower()/mb_strtoupper()

$str = "PHP 中文測試";
echo strtolower($str);       // php 中文測試(中文不變)
echo mb_strtolower($str);    // php 中文測試(同樣)
// 某些語系(如土耳其文)需指定編碼
echo mb_strtolower($str, 'UTF-8');

常見應用

  • 使用者輸入正規化(Email、帳號不分大小寫)
  • 關鍵字搜尋比對前統一大小寫
  • 產生統一格式的代碼、Slug

相關函數

  • ucfirst():首字母大寫
  • ucwords():每單字首字母大寫
  • lcfirst():首字母小寫

記得配合 mb_* 系列處理多語言環境。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved