ucfirst() 將字串第一個字元轉大寫;ucwords() 將字串中每個單字的第一個字元轉大寫(單字分隔以空白字元判斷)。
<?php $string = "php string functions are easy to use."; $sentence = ucfirst($string); $title = ucwords($string); echo "$sentence\n"; // Php string functions are easy to use. echo "$title\n"; // Php String Functions Are Easy To Use ?>
PHP 8.2+ ucfirst()/ucwords() 已支援 UTF-8;舊版請用 mb_convert_case():
$str = "php 字串 函數 測試"; echo mb_convert_case($str, MB_CASE_TITLE, "UTF-8"); // Php 字串 函數 測試
ucwords() 會將 "o'neil" 轉為 "O'Neil"(單引號後也視為單字開頭)善用兩函數快速產生標準標題格式。