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

PHP 常用函數速查總結:陣列、字串、時間、檔案操作

     編輯:PHP 相關知識

整理 PHP 開發最高頻的核心函數分類速查表,涵蓋陣列、字串、日期時間、檔案系統四大領域,適合日常開發快速查閱。

一、陣列函數【重點】

函數用途範例/備註
implode(glue, arr)陣列值連接為字串implode('-', [1,2,3]) → "1-2-3"
explode(sep, str)字串分割為陣列別名 join
array_merge(arr1, arr2...)合併陣列數值鍵重排,字串鍵後覆蓋前
array_combine(keys, values)建立關聯陣列array_combine(['a','b'], [1,2]) → ['a'=>1,'b'=>2]
array_push(arr, val...)壓入尾部(堆疊)返回新長度
array_unique(arr)移除重複值保留首次出現,索引保留
array_search(val, arr)搜尋值返回鍵嚴格比較用 in_array
array_values(arr)取得所有值(重排索引)
array_keys(arr)取得所有鍵
count(arr) / sizeof(arr)計算元素數
is_array(var)檢測是否陣列
sort(arr)排序(重排索引)引用修改,返回 bool
array_flip(arr)交換鍵值值需為 int/string,可用於去重
key(arr), current(arr), next(arr)內部指針操作配合 while 遍歷
reset(arr)指針歸零
array_chunk(arr, size, preserve_keys)分塊返回多維陣列

二、字串函數【重點】

函數用途
trim(str)移除首尾空白
strlen(str)位元組長度
substr(str, start, len)截取子字串
strstr(haystack, needle)首次出現位置到結尾
strpos(haystack, needle)首次出現位置索引
str_replace(search, replace, subject)字串替換
ucfirst(str)首字母大寫
strtolower/strtoupper大小寫轉換
ucwords(str)每個單詞首字母大寫
strcmp(str1, str2)二進制安全比較
substr_count(haystack, needle)子串出現次數

正則相關:

函數用途
preg_match(pattern, subject, &matches)單次匹配,$matches[0]完整,$matches[1]第一捕獲組
preg_match_all(pattern, subject, &matches)全局匹配
preg_replace(pattern, replacement, subject)搜尋替換
preg_split(pattern, subject)正則分割
preg_grep(pattern, array)陣列值匹配過濾

三、日期時間函數【重點】

date(format, timestamp) 格式化字元速查:

字元含義範例
Y4位年份2024
y2位年份24
m月份(帶前導0)01-12
n月份(無前導0)1-12
d日期(帶前導0)01-31
j日期(無前導0)1-31
F月份英文全名January
M月份縮寫Jan
H24小時(帶前導0)00-23
G24小時(無前導0)0-23
h12小時(帶前導0)01-12
g12小時(無前導0)1-12
i分鐘00-59
s秒00-59
aam/pm 小寫am
AAM/PM 大寫AM
L是否閏年0/1
l星期幾全名Monday
D星期縮寫Mon
w星期數字0(日)-6(六)
W年間週數01-53
t當月天數28-31
z年間天數0-365
cISO86012024-01-15T10:30:00+08:00
rRFC2822Mon, 15 Jan 2024 10:30:00 +0800
UUnix 時間戳1705285800

常用輔助函數:

  • checkdate(m, d, y):驗證日期合法性。
  • getdate(ts):時間戳轉關聯陣列。
  • mktime(h, i, s, m, d, y):構造時間戳。
  • strtotime(str):英文文字時間轉時間戳(支援 "today", "+3 days" 等)。
  • strftime(format, ts):本地化格式化(需 setlocale)。
  • microtime():微秒級時間戳(效能測試)。
  • gmdate/gmmktime:GMT 時間處理。
  • date_default_timezone_set/get:設定/獲取預設時區(PHP 5.1+)。

常用區間快捷計算:

$today = date("Y-m-d");
$yesterday = date("Y-m-d", strtotime("-1 day"));
$lastweek_start = date("Y-m-d", strtotime("monday last week"));
$lastweek_end = date("Y-m-d", strtotime("sunday last week"));
$thismonth_start = date("Y-m-01");
$thismonth_end = date("Y-m-t");
$thisquarter_start = date("Y-m-d", strtotime("first day of this quarter"));
$thisquarter_end = date("Y-m-d", strtotime("last day of this quarter"));

四、檔案操作【重點】

函數用途
file_exists(path)檔案/目錄是否存在
filesize(path)檔案大小(位元組)
pathinfo(path)解析路徑返回 ['dirname','basename','extension','filename']
mkdir(dir, mode, recursive)建立目錄
rmdir(dir)刪除空目錄
fopen(path, mode)開啟檔案指標
fclose(fp)關閉檔案指標
fwrite(fp, str, len)寫入檔案
file_put_contents(path, data, flags)一次寫入檔案(支援 FILE_APPEND, LOCK_EX)
file_get_contents(path)一次讀取檔案內容
fread(fp, len)讀取指定長度
fgets(fp)讀取一行
file(path)整個檔案讀入陣列(每行一元素)
readfile(path)讀取並直接輸出到輸出緩衝
copy(src, dest)複製檔案
unlink(path)刪除檔案
rename(old, new)重命名/移動檔案或目錄

編碼轉換: iconv('GBK', 'UTF-8', $str) 或 mb_convert_encoding($str, 'UTF-8', 'GBK')。

收藏建議

將此頁面加入書籤,遇到「這個函數怎麼用?」時秒級定位,提升編碼效率。

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