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

PHP 內建函數速查手冊:字串、陣列、檔案、資料庫、加密與效能優化常用函數分類整理

     編輯:PHP 相關知識

前言

PHP 擁有超過 1000 個內建函數,掌握高頻核心函數可大幅提升開發效率。本文將常用函數按功能領域分類整理,附上參數說明與實戰範例,適合做為開發時的速查參考。

一、字串處理函數

函數用途範例備註
strlen($str)取得位元組長度strlen("中文") // 6 (UTF-8)多位元組字串用 mb_strlen()
mb_strlen($str, 'UTF-8')取得字元數mb_strlen("中文", 'UTF-8') // 2需 mbstring 擴展
substr($str, $start, $len)截取子字串substr("abcdef", 2, 3) // "cde"負數從尾部算起
mb_substr()多位元組安全截取mb_substr("中文測試", 0, 2, 'UTF-8') // "中文"
strpos($haystack, $needle)尋找首次出現位置strpos("abc", "b") // 1區分大小寫,用 stripos() 不區分
str_replace($search, $replace, $subject)字串替換str_replace('a', 'b', 'abc') // "bbc"陣列支援批次替換
preg_replace($pattern, $replacement, $subject)正則替換preg_replace('/\d+/', '*', 'a1b2') // "a*b*"PCRE 正則
trim($str)移除首尾空白trim(" hi ") // "hi"第二參數指定字元集
explode($delimiter, $str)分割字串為陣列explode(',', 'a,b,c') // ['a','b','c']
implode($glue, $pieces)陣列合併為字串implode('-', [1,2,3]) // "1-2-3"參數順序可互換
htmlspecialchars($str, ENT_QUOTES, 'UTF-8')HTML 實體編碼 (防 XSS)htmlspecialchars('
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved