php分割字串函數_PHP教程

來源:互聯網
上載者:User
在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); //每個元素定長為4
print_r($result); //輸出轉換後的結果


?>

http://www.bkjia.com/PHPjc/631357.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631357.htmlTechArticle在php教程 中要分割字串常用的有二個函數,chunk_split,explode還有一個str_split函數,這個三了。下面看執行個體。 定義和用法 chunk_split() 函數...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.