PHP Regular Expression review (1)

Source: Internet
Author: User
Tags php regular expression
Let's review the common regular expression functions commonly used in PHP.

1. Preg_match () and Preg_match_all ()

int Prag_match ($pattern, $subject, optional parameter $matchs); Only one match is made, the return value 0 or 1, that is, the number of matches, the value to be matched into the third argument, is an array.

int Preg_match_all ($pattern, $subject, optional parameter $matchs); For all matches, the return value is also the number of matches, and the matching value is placed in the third parameter, which is a two-bit array.

2. Preg_replace () and Preg_filter ()

Preg_replace ($pattern Regular, $replacement the content to be replaced, $subject the target content); All three parameters can be arrays

Preg_filter (ibid.); ibid.

One difference between these two functions is that when the array is matched, the return value, Preg_filter () returns only the value that is replaced, and preg_replace () returns regardless of whether it is replaced or not.

Preg_grep (); match only, do not replace, return the match to the value. equals a reduced version of a preg_filter () feature.

3. Preg_split ($pattern, $subject);

Needless to say, an upgraded version of the explode () function.

4. Preg_quote ($STR);

You can escape the regular expression operator in a string by awakening it.

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