本文匯總 PHP 核心字符串函數 50+,按字母順序排列,適合快速查閱:
| 函數名 | 功能描述 |
|---|---|
addcslashes | 為指定字符添加反斜線轉義 |
addslashes | 按預定義方式轉義字符串 |
bin2hex | 二進制數據轉十六進制表示 |
chop | rtrim() 別名 |
chr | 返回 ASCII 碼對應字符 |
chunk_split | 按長度分割字符串 |
convert_cyr_string | 斯拉夫語字符集轉換 |
convert_uudecode | uudecode 解碼 |
convert_uuencode | uuencode 編碼 |
count_chars | 統計字符使用信息 |
crc32 | 計算 CRC32 多項式 |
crypt | 單向雜湊加密 |
echo | 輸出內容(語言構造非函數) |
explode | 分隔符分割字符串為數組 |
fprintf | 格式化寫入流 |
get_html_translation_table | 獲取 HTML 實體轉換表 |
hebrev / hebrevc | 希伯來文視覺轉換 |
html_entity_decode | HTML 實體轉字符(htmlentities 反函數) |
htmlentities | 字符轉 HTML 實體 |
htmlspecialchars_decode | 特殊實體轉字符(htmlspecialchars 反函數) |
htmlspecialchars | 特殊字符轉 HTML 實體 |
implode | 數組合併為字符串 |
join | implode 別名 |
levenshtein | 計算兩字符串編輯距離 |
localeconv | 獲取本地化數值格式信息 |
ltrim | 移除左側空白字符 |
md5 / md5_file | 計算 MD5 雜湊 |
metaphone | 計算 metaphone 鍵值 |
money_format | 格式化貨幣字符串 |
nl_langinfo | 查詢語言/地區信息 |
nl2br | 換行轉 <br> 標簽 |
number_format | 千分位格式化數字 |
ord | 返回字符 ASCII 值 |
parse_str | 解析查詢字符串到變量 |
print | 輸出字符串(語言構造) |
printf / sprintf | 格式化輸出/返回字符串 |
quoted_printable_decode | quoted-printable 解碼 |
quoted_printable_encode | quoted-printable 編碼 |
quotemeta | 轉義正則元字符 |
rtrim | 移除右側空白字符 |
setlocale | 設置地區信息 |
sha1 / sha1_file | 計算 SHA1 雜湊 |
similar_text | 計算字符串相似度 |
soundex | 計算 soundex 鍵值 |
sscanf | 格式化掃描字符串 |
str_getcsv | 解析 CSV 字符串為數組 |
str_ireplace | 大小寫不敏感替換 |
str_pad | 填充字符串至指定長度 |
str_repeat | 重複字符串 |
str_replace | 字符串替換 |
str_rot13 | ROT13 編碼 |
str_shuffle | 隨機打亂字符串 |
str_split | 字符串分割為數組 |
str_word_count | 統計單詞數 |
strcasecmp | 大小寫不敏感比較 |
strchr | strstr 別名 |
strcmp | 二進制安全比較 |
strcoll | 基於地區的字符串比較 |
strcspn | 獲取不包含指定字符的初始片段長度 |
strip_tags | 移除 HTML/PHP 標簽 |
stripcslashes | 反 addcslashes |
stripos | 大小寫不敏感位置查找 |
stripslashes | 反 addslashes |
stristr | 大小寫不敏感 strstr |
strlen | 字節長度 |
strnatcasecmp | 自然排序不敏感比較 |
strnatcmp | 自然排序比較 |
strncasecmp | 指定長度不敏感比較 |
strncmp | 指定長度二進制比較 |
strpbrk | 查找任一字符集字符位置 |
strpos | 查找首次出現位置 |
strrchr | 查找最後一次字符位置 |
strrev | 反轉字符串 |
strripos | 大小寫不敏感最後位置 |
strrpos | 查找最後一次出現位置 |
strspn | 獲取僅包含指定字符的初始片段長度 |
strstr | 查找首次出現並截取 |
strtok | 標記化分割字符串 |
strtolower | 轉小寫 |
strtoupper | 轉大寫 |
strtr | 字符轉換 |
substr | 提取子串 |
substr_compare | 指定位置比較子串 |
substr_count | 統計子串出現次數 |
substr_replace | 替換指定位置子串 |
trim | 移除首尾空白 |
ucfirst | 首字符大寫 |
ucwords | 每個單詞首字母大寫 |
vfprintf / vprintf / vsprintf | 可變參數格式化輸出 |
wordwrap | 按寬度換行 |
掌握高頻函數(strlen、substr、strpos、str_replace、explode/implode、trim、htmlspecialchars)可覆蓋 90%+ 日常開發場景。