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";
?>