PHP常用函式

2021-08-04 18:33:05 字數 3521 閱讀 2726

向上取整函式:ceil();

echo ceil(5/3);

輸出 2

向下取整函式:floor();

echo floor(5/3);

輸出 1

獲取unix時間戳 : int time()返回的是秒數

date(『y-m-d h:i:s』) 返回 : 2017-08-01 22:55:56

date(『y-m-d h:i:s』) 返回 :17-08-01 10:58:51

date(『y-m-d h:i:s』,$time) 可以將 \$time時間戳格式化

strtotime() 可以將任何英文文字的時間或日期轉換為時間戳

獲取今日凌晨時間戳strtotime(date(『y-m-d』,time()))

echo strtotime('17-08-01 00:00:00');

輸出1501516800

bool checkdate(month, day, year) 檢查時間的合法性

var_dump(checkdate(2,30,2017));

輸出bool(false)

intfile_put_contents(filename, data)寫入成功返回1

stringfile_get_contents(string filename ,[int offset [int maxlen]])

在filename中offset位置讀取maxlen長度的字串,讀取失敗返回false;

json_encode()將php變數(resource型別除外)轉換為json形式,失敗返回false;

json_decode()將json格式的字串轉換為php變數,當第二個引數為ture時,轉換為陣列(array),預設為物件(object)

json_encode 是編譯,json_decode是反編譯。注意:json只能接收utf-8編碼的字元,所以json_encode()引數必須是utf-8編碼,否則會得到空字串或null。

ios程式不識別json資料中\u開頭的資料,php生成json資料必須將漢字轉義為unicode編碼

json_encode( $data ,json_unescaped_unicode);
擴充套件:

前後端分離開發中,後端要返回給前端的永遠是json格式的資料

json書寫格式參考

$array = explode( separator \$string)

當乙個字串轉換為陣列時,需要指定分隔符(separator)切割

$string = implode( glue \$array)

當乙個陣列轉換為字串時,需要指定膠合劑(glue)粘合

雙引號 中的內容可以被解析,單引號只當作普通字元處理;

進行sql 查詢前,所有的字串都必須加單引號,以避免可以的注入漏洞和sql錯誤。

php中字串連線符是 『.』

trim()函式,用於去除字串首尾空格和特殊字元,

ltrim() 去除左邊的空格和特殊字元

rtrim() 去除右邊的空格和特殊字元

反斜線 『\』 對字串進行轉義

intstrlen( stringstr)

string length

stringsubstr( stringstr, intstart, [intlength])

函式中的start引數是從0開始計算的,所以字串中第乙個字元的位置為0 ,為負數時表示從倒數第 start 個字元開始向後擷取

length為負數是,表示從start擷取到倒數第length個字元空格也算乙個字元

漢字不同的編碼,占用的位元組不同,在擷取中文時,要注意文字編碼格式

start 和 length 只能有乙個為負數

echo substr('abcdefg',-4,2);    //輸出:de

echo substr('abcdefg',1,-1); //輸出:bcdef

按位元組比較: strcmp() 、 strcasecmp()

自然排序比較 : strnatcmp()

指定源字串的位置比較: strncmp()

注意!在計算機序列中:2>10 、a>a

1.strcmp() 和 strcasecmp()

2.strnatcmp()

- 格式:intstrnatcmp(stringstr1,stringstr2)

- 該函式是按照自然排序法進行比較,不同於計算機序列排序。

- 比較的是字串中的資料部分

- 區分大小寫,不區分的是strnatcasecmp()

3.strncmp()

1.strstr() 查詢指定關鍵字 ( 檢查字尾名等 )

- 格式:stringstrstr(stringstr,stringneedle)

- 在str中,查詢needle首次出現的位置,擷取從該位置到末尾的字串返回,失敗返回false

- 函式區分大小寫

- needle 匹配以 ascii值 為基礎

echo strstr('[email protected]','@'); //返回qq.com
2.substr_count() 子字串出現的次數

mixedstr_ireplace( mixedsearch, mixedreplace,mixedsubject[,int count])

php 採集常用函式 PHP常用採集函式

獲取所有鏈結內容和位址function getallurl code 獲取所有的位址 function getimgsrc code else fnum if fnum fnum 0 return fnum 去除html標記 function text2html txt isu r n txt re...

常用PHP函式

這是一些使用頻率比較高的函式,有的來自別人的程式.1.產生隨機字串函式 function random length return hash 2.擷取一定長度的字串 注 該函式對gb2312使用有效 function wordscut string,length sss 0 for i 0 i le...

PHP常用函式

很有用的一些函式,你可以作為原始碼儲存,然後以後避免重複編寫。檔案讀取函式 檔案讀取函式 function php read file name fclose fd return buf 檔案寫入函式 檔案寫入函式 function php write file name,data,method w...