php分割字符串函數
來源:程序員人生 發布時間:2013-10-05 15:25:11 閱讀次數:3968次
在php中要分割字符串常用的有二個函數,chunk_split,explode還有一個str_split函數,這個三了,下面看實例。
定義和用法
chunk_split() 函數把字符串分割為一連串更小的部分。
語法:chunk_split(string,length,end)
參數:string 必需,規定要分割的字符串。
參數:length 可選,一個數字,定義字符串塊的長度。
參數:end 可選,字符串值,定義在每個字符串塊之后放置的內容。
- */
- $data="hello world! this is a world!";
- $new_string=chunk_split($data);
- echo $new_string;
- /*
定義和用法
explode() 函數把字符串分割為數組。
語法:explode(separator,string,limit)
參數:separator 必需,規定在哪里分割字符串。
參數:string 必需,要分割的字符串。
參數:limit 可選,規定所返回的數組元素的最大數目。
- */
- $str='one|two|three|four';
- $result=explode('|',$str,2);
- print_r($result);
- $result=explode('|',$str,-1);
- print_r($result);
- /*
定義和用法
str_split() 函數把字符串分割到數組中。
語法:str_split(string,length)
參數:string 必需,規定要分割的字符串。
參數:length 可選,規定每個數組元素的長度,默認是 1。
- $str="hello world";
- $result=str_split($str);
- print_r($result);
- $result=str_split($str,4);
- print_r($result);
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈