- $ Date = "08-30-2010 ";
- // The delimiter can be a diagonal line, a dot, or a horizontal line.
- List ($ month, $ day, $ year) = split ('[/.-]', $ date );
- // The output is in another time format.
- Echo "today is: $ year, $ month, $ day ";
- ?>
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. You 2 split the first four fields in/etc/passwd:
-
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. Example 3: parse a date that may be separated by diagonal lines, points, or horizontal lines:
- // 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 ";
- ?>
-
Note: 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 document is in the format of the manual page. you can read it using commands similar to man/usr/local/src/regex/regex.7. Split a piece of text by words, because the text may be a mix of Chinese and English, and the php function strlen can only calculate the number of bytes of the string, so I implemented several functions and shared them. Example 1: calculate the total length of a character.
0xa0) {$ ccLen ++; $ ind ++;} else {$ ccLen ++;} return $ ccLen ;}?> Example 2: extract a string from the left.
Example 3: store the given text into an array based on the number of slices (suitable for short text, which can be directly processed once if a long article is not separated)
0) {$ str_tmp = ccStrLeft ($ str_tmp, $ smslen); array_push ($ arr_cont, $ str_tmp); $ I + = strlen ($ str_tmp ); $ str_tmp = substr ($ content, $ I, strlen ($ content);} return $ arr_cont;} // by bbs.it-home.org?> Test:
'; $ Smslen = 3; // capture length print_r (SplitContent ($ str, $ smslen);?> Split result: array ([0] => a computing [1] => Chinese and English [2] => mixed 1 [3] => 234 [4] => string [5] => length [6] => abc [7] => d) |