php分割函數preg_split()函數、用法、例子、參數說明

來源:互聯網
上載者:User

php分割函數preg_split()函數、用法、例子、參數說明

preg_split

定義和用法

preg_split() 函數通過一個Regex分割字串。

相關函數:spliti(),str_split(),explode(),implode(),chunk_split()

文法

preg_split(pattern,subject,limit,flags)
參數 描述
pattern Regex
subject 待分割字串
limit 限制分割得到的子串最多隻有limit個
flags PREG_SPLIT_NO_EMPTY
如果這個標記被設定, preg_split() 將進返回分隔後的非空部分。
PREG_SPLIT_DELIM_CAPTURE
如果這個標記設定了,用於分隔的模式中的括號運算式將被捕獲並返回。
PREG_SPLIT_OFFSET_CAPTURE
如果這個標記被設定, 對於每一個出現的匹配返回時將會附加字串位移量. 注意:這將會改變返回數組中的每一個元素, 使其每個元素成為一個由第0 個元素為分隔後的子串,第1個元素為該子串在subject 中的位移量組成的數組。

例子

preg_split() 擷取搜尋字串的部分


輸出:

Array(    [0] => hypertext    [1] => language    [2] => programming)

將一個字串分隔為組成它的字元


輸出:

Array(    [0] => s    [1] => t    [2] => r    [3] => i    [4] => n    [5] => g)

分隔一個字串並擷取每部分的位移量


輸出:

Array(    [0] => Array        (            [0] => hypertext            [1] => 0        )    [1] => Array        (            [0] => language            [1] => 10        )    [2] => Array        (            [0] => programming            [1] => 19        ))
  • 聯繫我們

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