Usage of the php split () function

Source: Internet
Author: User
Tags expression engine

The basic syntax of the PHP function split () is array split (string $ pattern, string $ string [, int $ limit]). We will give you two examples to illustrate how to use this function.

For beginners, understanding the usage of common functions in PHP is the basis for their continued learning. Today, we will introduce you in detail how to use the PHP function split (). I hope you can add your own knowledge base through the content introduced in this article.
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 ()
In-depth explanation of PHP nl2br () Formatting output
Copy codeThe Code is as follows:
<? Php 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 ()
Copy codeThe Code is as follows:
<? 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 () 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.

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.