PHP Regular Expression learning notes _php tips

Source: Internet
Author: User
Tags php regular expression

PHP Regular Expressions Learn note sharing:

1. Creating Regular Expressions

$regex = '/\d/i ';
It's a bit like the first way in JavaScript, but the word here is a string.

2. Special characters in regular expressions

Special characters are: . \ + *? [ ^ ] $ ( ) { } = ! < > | : -

3. Functions in regular expressions

There are 8 methods,Preg_match and Preg_match_all,preg_replace and Preg_replace_callback,preg_grep, Preg_split, Preg_last_ Error and Preg_quote.

Preg_match:

Performs a regular expression match

returns the number of matches for pattern. Its value will be 0 times (mismatch) or 1 times, because Preg_match () will stop the search after the first match .

$subject = "Dd133aa2";
$pattern = '/\d+/';
Preg_match ($pattern, $subject, $matches, preg_offset_capture);
Print_r ($matches);

The example code above adds the parameter "preg_offset_capture" so that there will be an extra offset in the $matches. For example, the following "2"

Preg_match_all:

Performs a "global" regular expression match

Returns the full number of matches (possibly 0), or False if an error occurs.

The following code $subject and $pattern are the same as the above, the only difference is that Preg_match replaced by Preg_match_all.

Preg_match_all ($pattern, $subject, $matches, preg_offset_capture);
Print_r ($matches);

The number returned is 2, matched to two, and then the input in the array $matches, two. One more than the above.

preg_replace:

Performs search and replace of a regular expression

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

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

In code, $subject is an array with two sets of strings, followed by the substitution of multiple digits with uppercase "Z".

$subject = Array ("Dd133aa2", "KK1FF3");
$pattern = '/\d+/';
$result = Preg_replace ($pattern, ' Z ', $subject);
Print_r ($result);

Preg_replace_callback:

Executes a regular expression search and replaces it with a "callback"

The behavior of this function, in addition to specifying a callback substitution replacement for a replacement string, is equivalent to Preg_replace (), including the returned result.

The following code is also replaced with uppercase "Z", in which the contents of each $matches in the callback function are the part of the annotation in the code, the first time is 133, and the second is 2.

$subject = "Dd133aa2";
$pattern = '/\d+/';
$result = Preg_replace_callback ($pattern, function ($matches) {
    //$matches [0] =>
    //$matches [0] => 2 Return
    ' Z ';
   }, $subject);

Preg_grep:

Returns an array entry for a matching pattern

Returns an array that is indexed using the key in input.

In the example code below, I added a "DDSDFD" in the $subject array, which doesn't contain numbers, and when I do a match, I filter out the numbers.

and $RESULT2 print out exactly the opposite, is the filter printed out, but the key is still 2, not 0.

$subject = Array ("Dd133aa2", "KK1FF3", "DDSDFD");
$pattern = '/\d+/';
$result = Preg_grep ($pattern, $subject);
$result 2 = Preg_grep ($pattern, $subject, Preg_grep_invert);
Print_r ($result);
Print_r ($result 2);


Preg_split:

Separating strings with a regular expression

Returns an array of substrings that are separated by a pattern boundary subject.

In the following code, I add parentheses to the expression in $pattern in order to capture it in $RESULT2.

$subject = "dd133aa2cc";
$pattern = '/(\d+)/';
$result = Preg_split ($pattern, $subject);
$result 2 = Preg_split ($pattern, $subject, NULL, preg_split_delim_capture);
Print_r ($result);
Print_r ($result 2);


Preg_last_error:

Returns the error code generated by the last Pcre execution

Preg_match ('/(?: \ d+|<\d+>) *[!?] /', ' foobar foobar foobar ');
$result = Preg_last_error ();//preg_backtrack_limit_error call backtracking limit beyond
Print_r ($result);

Preg_quote:

Escape Regular Expression characters

Returns the escaped string .

In the following code, there are two characters in the $subject that need to be escaped, "." and "?".

The $result print out is "dd\.a\?a2cc", and in $result2, an extra parameter "a" is added, so that "a" is also escaped, "dd\.\a\?\a2cc"

$subject = "dd.a?a2cc";
$result = Preg_quote ($subject);
$result 2 = preg_quote ($subject, ' a ');
Print_r ($result);
Print_r ($result 2);

4, Mode modifier

The above is about the PHP regular expression of all the contents of the introduction, I hope that the learning of all help.

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.