Differences between PHP split and explode

Source: Internet
Author: User
Tags expression engine
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.