Correct use of PHP function preg_split

Source: Internet
Author: User
Tags php regular expression
ArticleDirectory
    • Correct use of PHP function preg_split
Correct use of PHP function preg_split

Name: csdn () font size: T | T

The syntax of the PHP function preg_split is array preg_split (string $ pattern, string $ subject [, int $ limit [, int $ flags]). We will give an example to describe the usage of this function in detail.

AD:

    For beginners, you do not have a thorough understanding of the PHP function preg_split, because it is complicated and difficult to use. Today we will introduce it to you in detail, hoping to help readers better understand the PHP function preg_split.

      • In-depth discussion on how PHP deletes database tables
      • In the string replacement operation
      • PHP Regular Expression preg_match rules
      • Introduction to the definition and syntax of PHP function trim ()
      • How to format a string using the PHP function sprintf ()

    Description: array preg_split (string $ pattern, string $ subject [, int $ limit [, int $ flags])

    Returns an array that contains the substrings separated by subject along the boundary matching pattern.

    If the limit parameter is specified, a maximum of limit substrings are returned. If the limit parameter is-1, there is no limit. It can be used to specify the optional parameter flags.

    Flags can be any combination of the following tags (using bitwise OR operator | combination ):

    Preg_split_no_empty
    If this flag is set, preg_split () returns only non-null components.
    Preg_split_delim_capture
    If this mark is set, the parentheses expression in the delimiter mode will also be captured and returned. This tag is added to PhP 4.0.5.
    Preg_split_offset_capture
    If this tag is set, the offset of the affiliated string is also returned for each matching result. Note that this changes the value of the returned array, so that each unit is also an array. The first item is the matching string, and the second item is its offset in the subject. This tag is available from PhP 4.3.0.

    Prompt

    If you do not need the regular expression function, you can choose to use a faster (or simpler) Alternative function, such as explode () or str_split ().

    Example 1672. php function preg_split: Obtain the search string component

     
      
      
    1. // Split the phrase by any number of commas or space characters,
    2. // Which include "", \ r, \ t, \ n and \ f
    3. $ KEYWORDS = preg_split ("/[\ s,] +/", "hypertext language, programming ");
    4. ?>

    For example, the 1673. php function preg_split Splits a string into characters.

      
      
    1. $ STR = 'string ';
    2. $ Chars = preg_split ('//', $ STR,-1, preg_split_no_empty );
    3. Print_r ($ chars );
    4. ?>

    Example 1674. php function preg_split Splits a string into a match and Its offset

     
      
      
    1. $ STR = 'string ';
    2. $ Chars = preg_split ('//', $ STR,-1, preg_split_no_empty );
    3. Print_r ($ chars );
    4. ?>

    The PHP function preg_split example will be output:

     
      
      
    1. Array ([0] =>
    2. Array ([0] => hypertext [1] => 0) [1] =>
    3. Array ([0] => language [1] => 10) [2] =>
    4. Array ([0] => programming [1] => 19 ))

    [Responsible editor: Cao kai]

    Address: http://developer.51cto.com/art/200911/165373.htm

    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.