PHP Summary Regular expression correlation function usage

Source: Internet
Author: User

Take a look at the regular expressions in depth today and summarize the functions that PHP's regular expressions use

Preg_match ();
Preg_match_all ();
Preg_replace ();
Preg_filter ();
Preg_grep ();
Preg_split ();
Preg_quote ();

There are basically so many.

Come on.

Let me introduce you to a tool for online validation of regular expressions

http://regexpal.isbadguy.com/

Open it and you'll see how it's going to work.

The Preg_match () function is used to match a regular expression, successfully returning 1, otherwise returning 0.

This is very simple, just an example to understand.

$subject = "abcdef";
$pattern = '/^def/';
Preg_match ($pattern, $subject, $matches, Preg_offset_capture, 3);
Print_r ($matches);
The Preg_match_all () function is used to make a regular expression global match, successfully returning the number of times the entire pattern match (possibly 0), or FALSE if the error returns.

Preg_match_all ("|<[^>]+> (. *) </[^>]+>| U ",
"<b>example: </b><div align=left>this is a test</div>",
$out, Preg_pattern_order);
echo $out [0][0]. ", " . $out [0][1]. "\ n";
echo $out [1][0]. ", " . $out [1][1]. "\ n";

Let's focus on the story.

Preg_replace ();

Preg_filter ();

The difference between the two functions

$subject = Array (' 1 ', ' A ', ' 2 ', ' B ', ' 3 ', ' A ', ' B ', ' 4 ');
$pattern = Array ('/d/', '/[a-z]/', '/[1a]/');
$replace = Array (' a:$0 ', ' b:$0 ', ' c:$0 ');
echo "Preg_filter returns";
Print_r (Preg_filter ($pattern, $replace, $subject));
echo "Preg_replace returns";
Print_r (Preg_replace ($pattern, $replace, $subject));

Obviously, we can look at the results.

Preg_filter returns Array
(
[0] => c:1
[1] => b:c:a
[3] => b:b
)
Preg_replace returns Array
(
[0] => c:1
[1] => b:c:a
[2] => 2
[3] => b:b
[4] => 3
[5] => A
[6] => B
[7] => 4
)
This makes it clear that Preg_filter does not retain the mismatched option, and Preg_replace retains the mismatched option

preg_grep-returns an array entry for the matching pattern

$array = Array ("23.32", "22", "12.009", "23.43.43");
Print_r (Preg_grep ("/^ (\d+) \.\d+\.\d+$/", $array));

Preg_split-a regular expression to separate strings

The point of this function is that explode () can be considered a subset of this function.

Preg_quote-Escape Regular Expression characters

This last function is nothing to talk about. is to not let the execution of the regular expression, escape the character, and the manual plus transfer is the same

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.