Summary of regular processing functions for PHP Regular Expressions (preg_match,preg_match_all,preg_replace,pr_php tutorial

Source: Internet
Author: User
We have already learned the basic syntax of regular expressions, including delimiters, atoms, metacharacters, and pattern modifiers. In fact, if the regular expression wants to function, it must be borrowed from the regular expression handler. In this section we will introduce the Perl-based regular expression processing functions in PHP, mainly including the segmentation, matching, find, replace and so on processing operations, is still a sample to explain, let us begin.

Like regular expressions, regular expression handlers cannot be used independently, and this must be combined to accomplish specific functions. As we have said earlier, Perl-based regular expressions are faster than poxis regular expression handlers, so we only introduce Perl-based regular expressions that start with preg. Note: When you can even use string functions, do not use regular expressions to handle strings, because string processing functions are faster.

Let's look at some commonly used regular expression handlers.

The 1,preg_match () function.

The function Preg_match () performs a regular expression match, which is defined as follows:

int Preg_match (string $pattern, String $subject [, Array & $matches [, int $flags = 0 [, int $offset = 0]])

is actually searching for the part of the subject that matches the pattern to be saved in the array matches. See Example:
Copy CodeThe code is as follows:
$pattern = '/ . *?<\/b>/';
$string = 'Welcome to PhpfunsDsadsadas ';
if (Preg_match ($pattern, $string, $arr)) {
echo "Regular expression{$pattern}and string{$string}Match success
";
Print_r ($arr);
} else {
echo "Regular expression {$pattern} and string {$string} match failed";
}
?>


The 2,preg_match_all () function.

The function Preg_match_all () function performs a global regular expression match whose definition is identical to the Preg_match () function, except that it matches all of the results. Take a look at the example:
Copy CodeThe code is as follows:
$pattern = '/. *?<\/b>/';
$string = 'Welcome to PhpfunsDsadsadas ';
if (Preg_match_all ($pattern, $string, $arr)) {
echo "Regular expression{$pattern}and string{$string}Match success
";
Print_r ($arr);
} else {
echo "Regular expression {$pattern} and string {$string} match failed";
}
?>

Is still the above example (only the regular handler function is Preg_match_all ()), but the matching result array contents are different.

3, Preg_replace () function

The function preg_replace () performs a regular expression substitution, which is defined as follows:

Mixed preg_replace (mixed $pattern, mixed $replacement, mixed $subject [, int $limit =-1 [, int & $count]])

It's actually searching for the part of the subject that matches the pattern, replacing it with replacement. Limit refers to the maximum number of times each pattern is replaced on each subject. The default is-1 (infinity). If count is specified, it is populated with the complete number of replacements.

Note:

A, if subject is an array, preg_replace () returns an array, and in other cases returns a string.

B, if the match is found, the replaced subject is returned, and other cases return unchanged subject. If an error occurs, NULL is returned.

C, the sub-mode can be applied to the parameter replacement, using \ n or ${n}. (in the pattern of regular expressions we can only use \ n to get the matching sub-pattern, remember!) )

D, if the pattern modifier e is used, the function can be parsed in parameter replacement. (in other regular expression handlers, the pattern modifier e is ignored!)

take a look at the following comprehensive example:
Copy CodeThe code is as follows:
$pattern = '/(PHP) | (MySQL)/E ';
$string = ' php and MySQL in this string have been replaced with uppercase! ';
$result = Preg_replace ($pattern, ' Strtoupper ("${1}\2") ', $string,-1, $count);
echo $result. '
';
Echo $count;
?>

In the above example, we used the pattern modifier e, so that the Strtoupper () function can be parsed as a string, which is the function of the pattern modifier E! The parameters ${1} and \2 are sub-mode 1 and sub-mode 2 respectively. The effect of the example above is to replace the substring in string $string with the sub-schema PHP and mysql into uppercase letters!

The 4,preg_split () function.

Preg_split executes a regular expression-delimited string. It is defined as follows:

Array Preg_split (String $pattern, string $subject [, int $limit =-1 [, int $flags = 0]])

In effect, the subject is separated by the pattern, returning the segmented array. Where limit restricts the number of substrings that can be delimited to only a limit, the last substring returned will contain all the remainder. A limit value of 1, 0, or null represents "unrestricted."

Let's look at an example:
Copy CodeThe code is as follows:
$pattern = '/

(.*?) <\/p>/';
$string = ' In this string

Php

And

Mysql

It's been split! ';
$result = Preg_split ($pattern, $string,-1, preg_split_delim_capture);
Print_r ($result);
?>

In the example above, we used the constant Preg_split_delim_capture setting to return the result with a sub-pattern (if set to Preg_split_no_empty,preg_split () to return the non-empty part after the separation. If we remove the parentheses from the previous example, the results will no longer contain both PHP and MySQL matching successful sub-patterns.

Commonly used regular expression handlers we're done here, and the examples in this section may be difficult, but hopefully we'll try and experience it carefully, and we'll often use regular expression handlers as part of the regular expression application.

http://www.bkjia.com/PHPjc/326078.html www.bkjia.com true http://www.bkjia.com/PHPjc/326078.html techarticle we have already learned the basic syntax of regular expressions, including delimiters, atoms, metacharacters, and pattern modifiers. In fact, if the regular expression wants to play a role, it must be borrowed ...

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