php字串的有序拆分的實現步驟

來源:互聯網
上載者:User
今天給大家說幾個函數,讓我們一起來實現php字串的有序拆分的實現步驟,下面請看案列

chunk_split() :函數把字串分割為一連串更小的部分。explode():使用一個字串分割另一個字串str_split():將字串分割到數組中chunk_split()

文法

chunk_split(string,length,end)

參數描述

string必需。規定要分割的字串。

length可選。數字值,定義字串塊的長度。預設是 76。

end可選。字串值,定義在每個字串塊末端放置的內容。預設是 \r\n。

<!--?php$str = "Shanghai";echo chunk_split($str,1,".");?-->輸入結果:S.h.a.n.g.h.a.i.explode()

本函數為 implode() 的反函數,使用一個字串分割另一個字串,返回一個數組。

array explode( string separator, string string [, int limit] )

參數說明

separator分割標誌

string需要分割的字串

limit可選,表示返回的數組包含最多 limit 個元素,而最後那個元素將包含 string 的剩餘部分,支援負數。

<!--?php$str = 'one|two|three|four';print_r(explode('|', $str));print_r(explode('|', $str, 2));// 負數的 limit(自 PHP 5.1 起)print_r(explode('|', $str, -1));?-->

輸出結果如下:

Array(    [0] => one    [1] => two    [2] => three    [3] => four)Array(    [0] => one    [1] => two|three|four)Array(    [0] => one    [1] => two    [2] => three)str_split()

str_split() 將字串分割為一個數組,成功返回一個數組。

?1array str_split( string string [, int length] )

參數說明

string需要分割的字串

length可選,表示每個分割單位的長度,不可小於1

例子:

<!--?php$str = 'one two three';$arr1 = str_split($str);$arr2 = str_split($str, 3);print_r($arr1);print_r($arr2);?-->

輸出結果如下:

Array(    [0] => o    [1] => n    [2] => e    [3] =>      [4] => t    [5] => w    [6] => o    [7] =>      [8] => t    [9] => h    [10] => r    [11] => e    [12] => e)Array(    [0] => one    [1] =>  tw    [2] => o t    [3] => hre    [4] => e)

相信看了這些案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

相關閱讀:

駝峰命名與JS的問題解答

JS的冒泡事件如何使用

JS裡的布爾值、關係運算子、邏輯運算子的詳解及執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.