PHP 擁有超過 100 個字串處理函數,本文按功能分類提供完整導航,幫助開發者快速定位所需函數。
| 函數 | 功能 | 範例 |
|---|---|---|
trim() | 移除首尾空白字符 | trim(" hello ") → "hello" |
ltrim() | 移除左側空白 | |
rtrim() | 移除右側空白 | |
chop() | rtrim() 別名 | |
str_pad() | 填充字串至指定長度 | str_pad("5", 3, "0", STR_PAD_LEFT) → "005" |
str_repeat() | 重複字串 | str_repeat("-", 10) |
wordwrap() | 斷行包裝 | wordwrap($text, 80, "\n") |
chunk_split() | 分塊分割 | chunk_split(base64_encode($data), 76, "\n") |
| 函數 | 功能 |
|---|---|
strtolower() | 全小寫 |
strtoupper() | 全大寫 |
ucfirst() | 首字符大寫 |
ucwords() | 每個單詞首字母大寫 |
lcfirst() | 首字符小寫 |
mb_strtolower() | 多位元組安全版 |
htmlspecialchars(), htmlentities()mysqli_real_escape_string(), PDO 預處理addslashes(), stripslashes()urlencode(), urldecode(), rawurlencode()mb_convert_encoding(), iconv()strcmp(), strcasecmp()strnatcmp(), strnatcasecmp()strncmp(), strncasecmp()similar_text(), levenshtein()strpos(), stripos(), strrpos(), strripos()strstr(), stristr(), strchr(), strrchr()substr_count()preg_match(), preg_match_all()str_replace(), str_ireplace()substr_replace()strtr()preg_replace(), preg_replace_callback()str_rot13()explode()preg_split()str_split(), mb_str_split() (PHP 7.4+)parse_str()implode(), join()str_getcsv(), fgetcsv(), fputcsv()printf(), sprintf(), vprintf(), vsprintf()number_format():數字千分位格式化money_format():貨幣格式化 (需系統支援)| 函數 | 功能 |
|---|---|
md5(), sha1(), hash() | 雜湊摘要 |
crypt(), password_hash() | 密碼雜湊 (推薦 password_hash) |
crc32() | 循環冗餘校驗 |
metaphone(), soundex() | 發音相似編碼 |
parse_url() | URL 解析 |
http_build_query() | 陣列構建查詢字串 |
nl2br() | 換行轉 <br> |
strip_tags() | 移除 HTML 標籤 |
addcslashes(), stripcslashes() | C 風格轉義 |