Explore how PHP functions split () use regular expressions to cut strings. For beginners, understanding the usage of common functions in PHP is the basis for their continued learning. Today, we will introduce arraysplit (string $ pattern, string $ s) in detail. for beginners, understanding the usage of common functions in PHP is the basis for their continued learning. Today we will give you a detailed introduction.
Description
Array split (string $ pattern, string $ string [, int $ limit])
Prompt
The preg_split () function uses Perl-compatible regular expression syntax, which is usually a faster alternative than the PHP function 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.
Separate the first four fields in/etc/passwd:
Example 1839. PHP function split ()
-
- list($user, $pass, $uid, $gid, $extra) =
- split (":", $passwd_line, 5);
- ?>
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 1840. PHP function split ()
-
- // 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
N ";
- ?>
For examples of the behavior similar to @ chars = split ('', $ str) in Perl, see the example in the preg_split () or str_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 PHP function split () (or any other regex function) is abnormal, read the regex.7 file contained in the regex/subdirectory of the PHP release package. This file is in the manual page format and can be read using commands similar to man/usr/local/src/regex/regex.7.
Bytes. Today we will give you a detailed description of array split (string $ pattern, string $ s...