Split
(PHP 3, PHP 4)
Split -- use a regular expression to split a string into an array
Description
Array split (string pattern, string [, int limit])
Tip: The preg_split () function uses Perl-Compatible Regular Expression syntax, which is usually a faster alternative than split. If you do not need the power of a regular expression, the use of explode () is faster, so that the regular expression engine will not be wasted.
This function returns a string array. Each unit is a string. The case-sensitive Regular Expression Pattern serves as a substring separated by the boundary. If limit is set, the returned array contains up to limit units, and the last unit contains all the remaining parts of the string. If an error occurs, split () returns false.
If the string contains N items that match the pattern, the returned array contains N + 1 units. For example, if no pattern is found, an array with only one unit is returned. Of course, this is also true if the string is empty.
Parsing dates that may be separated by diagonal lines, points, or horizontal lines:
Example 2. Split () Example
<? PHP
// The delimiter can be a diagonal line, a dot, or a horizontal line.
$ Date = "04/30/1973 ";
List ($ month, $ day, $ year) = Split ('[/.-]', $ date );
Echo "month: $ month; Day: $ day; year: $ year <br/> \ n ";
?>
For examples of the behavior similar to @ chars = Split ('', $ Str) in Perl, see the example in the preg_split () function.
note that pattern is a regular expression. If you want to use a delimiter that is a special character in a regular expression, you must first escape it. If the split () (or any other RegEx function) behavior is odd, read the regex.7 file contained in the RegEx/subdirectory of the PHP release package. This document is in the format of the manual page. You can read it using commands similar to man/usr/local/src/RegEx/regex.7.