PHP split 函數

來源:互聯網
上載者:User

PHP中split 函數的文法是: array split ( string $pattern , string $string [, int $limit ] )

split()函數返回一個字串數組,每個單元為$string經Regex$pattern作為邊界分割出的子串。如果設定了$limit,則返回的數組最多包含$limit個單元,而其中最後一個單元包含了$string中剩餘的所有部分。

pattern:用於指定作為分解標識的符號,注意該參數區分大小寫。

$string: 用於被處理的字串。

limit:返回分解子串個數的最大值,預設時為全部返回。


例子
#1 split() example

把字串如:”1:0:1:0:1″存入數組 再輸出來
$str=”1:0:1:0:1″;
$arraylist=split(“:”,$str); //存入數組

for($i=0;$i<count($arraylist);$i++) //把它們全部輸出來
{
echo $arraylist[$i].”   “;
}

#2 split() example

To split off the first four fields from a line from /etc/passwd:

//把/ect/password分為五部分,即前四份和最後一段

<?php
list($user, $pass, $uid, $gid, $extra) =
split(":", $passwd_line, 5);
?>
#3 split() example

To parse a date which may be delimited with slashes, dots, or hyphens:

//用斜幹,點,橫線把日期分開

<?php
// Delimiters may be slash, dot, or hyphen
$date = "04/30/1973";
list($month, $day, $year) = split('[/.-]', $date);
echo "Month: $month; Day: $day; Year: $year<br />\n";
?>

聯繫我們

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