Perl-compatible regular expression functions (1)

Source: Internet
Author: User
String match and search preg_match () and preg_match_all () functions the syntax format of this function is as follows: intpreg_matchpreg_match_all (stringpattern, stringsubject [, arraymatches]) string match and search

Preg_match () and preg_match_all () functions

The syntax format of this function is as follows:

Int preg_match/preg_match_all (string pattern, string subject [, array matches])

Function:

Match the expression pattern in the string subject. The number of matching times returned by the function. If an array matches exists, the matching results are stored in the array matches.

This function has two required parameters. The first parameter, pattern, needs to provide the mode written by the user according to the regular expression syntax. The second parameter, subject, requires a string. This function is used to search for the content that matches the regular expression given by the first parameter in the second string parameter. If matches is provided as the third optional array parameter, it can be used to save the matching results for each part of the child pattern in the first parameter. The subpattern in a regular expression is a pattern unit enclosed by parentheses (). matches [0], the first element in the array, stores the overall content that matches the regular expression pattern. Other elements in the array matches store the matching content with the subexpressions in the parentheses of the regular expression in sequence. For example, matches [1] stores the content that matches the first parentheses in the regular expression, matches [2] stores the content that matches the second parentheses in the regular expression, and so on. This function only performs one match and returns the number of matching results of 0 or 1.

Instance display:

 'var_dump($str1);$num2 = petg_match_all($preg,$str,$str2);echo $num2.'
'var_dump($str2);?>

Use the. preg_match () and preg_match_all () functions to match the string $ str and return the matching times.

Preg_quote () function

The syntax of the function is as follows:

string preg_quote(string str[,string delimiter])

Function: This function automatically escapes all special characters in the str string. If the delimiter parameter exists, this parameter

The contained strings are also escaped. The function returns the escaped string.

Instance display:

 ';$str2 = 'b';$match_one = preg_quote($str,$str2);echo $match_one;?>

Note: Special characters here refer to metacharacters of certain significance in regular expressions. Others such as "@" and "#" won't be processed as special characters.

The above is a detailed description of Perl-compatible regular expression functions (1). For more information, see other related articles in the first PHP community!

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.