php字串分割(explode str_split preg_split

來源:互聯網
上載者:User

php教程 explode() 函數
php string 函數
定義和用法
explode() 函數把字串分割為數組。

文法
explode(separator,string,limit)參數 描述
separator 必需。規定在哪裡分割字串。
string 必需。要分割的字串。
limit 可選。規定所返回的數組元素的最大數目。

例子
在本例中,我們將把字串分割為數組:

 代碼如下 複製代碼
<?php
$str = "hello world. it's a beautiful day.";
print_r (explode(" ",$str));
?>

輸出:

 代碼如下 複製代碼
array
(
[0] => hello
[1] => world.
[2] => it's
[3] => a
[4] => beautiful
[5] => day.
)

str_split 拆分函數

定義和用法
str_split() 函數把字串分割到數組中。

文法
str_split(string,length)參數 描述
string 必需。規定要分割的字串。
length 可選。規定每個數組元素的長度。預設是 1。

說明
如果 length 小於 1,str_split() 函數將返回 false。

如果 length 大於字串的長度,整個字串將作為數組的唯一元素返回。
例子
例子 1

 代碼如下 複製代碼
<?php
print_r(str_split("hello"));
?>

輸出:

 代碼如下 複製代碼
array
(
[0] => h
[1] => e
[2] => l
[3] => l
[4] => o
)

例子 2

 代碼如下 複製代碼
<?php
print_r(str_split("hello",3));
?>

輸出:

 代碼如下 複製代碼
array
(
[0] => hel
[1]


preg_split -- 用Regex分割字串
說明
array preg_split ( string pattern, string subject [, int limit [, int flags]])


返回一個數組,包含 subject 中沿著與 pattern 匹配的邊界所分割的子串。

如果指定了 limit,則最多返回 limit 個子串,如果 limit 是 -1,則意味著沒有限制,可以用來繼續指定選擇性參數 flags。

flags 可以是下列標記的任意組合(用按位或運算子 | 組合):


preg_split_no_empty
如果設定了本標記,則 preg_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.