How to use regular expressions to cut strings using PHP function split ()

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

 
 
  1. list($user, $pass, $uid, $gid, $extra) =
  2. split (":", $passwd_line, 5);
  3. ?>

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 ()

 
 
  1. // The delimiter can be a diagonal line, a dot, or a horizontal line.
  2. $ Date = "04/30/1973 ";
  3. List ($ month, $ day, $ year) = split ('[/.-]', $ date );
  4. Echo "Month: $ month; Day: $ day; Year: $ year
    N ";
  5. ?>

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...

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.