Explore PHP function split () How to use regular expressions to cut strings _php tutorial

Source: Internet
Author: User
Tags expression engine
For beginners, mastering the usage of common functions in PHP is the basis of their continued learning. Today we will give you a detailed introduction about Description
Array split (string $pattern, string $string [, int $limit])

Tips
The Preg_split () function uses Perl-compatible regular expression syntax, which is usually a faster alternative to split () than the PHP function. If you don't need the power of a regular expression, use explode () faster, so you don't incur the waste of the regular expression engine.

This function returns an array of strings, each of which is a string with a case-sensitive regular expression pattern as a substring of the boundary. If limit is set, the returned array contains a maximum of limit cells, and the last cell contains all the remaining parts of the string. If there is an error, split () returns FALSE.

Split the first four fields in the/etc/passwd:

Example 1839. php function split () example

 
  
  
  1. Php
  2. List ($user, $pass, $uid, $gid, $extra) =
  3. Split (":", $passwd _line, 5);
  4. ?>

If there are n items in a string that match pattern, the returned array will contain n+1 cells. For example, if pattern is not found, an array with only one cell is returned. Of course, this is true if the string is empty.

Parse the date that may be split with a slash, dot, or horizontal line:

Example 1840. php function split () example

 
  
  
  1. Php
  2. The delimiter can be a slash, a dot, or a horizontal line
  3. $ Date "04/30/1973";
  4. List ($month, $day, $year) = Split (' [/.-] ', $date);
  5. echo "Month: $month; Day: $day; Year: $year<br/>n ";
  6. ?>

To emulate a similar @chars = Split (", $STR) behavior in Perl, refer to the example in the Preg_split () or Str_split () function.

Note that pattern is a regular expression. If you want to use a split character that is a special character in a regular expression, first escape it. If you feel that the PHP function split () (or any other Regex function) behaves strangely, read the regex.7 file contained in the regex/subdirectory of the PHP release package. This file is a manual page format and can be read with commands like man/usr/local/src/regex/regex.7.


http://www.bkjia.com/PHPjc/446296.html www.bkjia.com true http://www.bkjia.com/PHPjc/446296.html techarticle for beginners, mastering the usage of common functions in PHP is the basis of their continued learning. Today we will give you a detailed description of the instructions in 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.