Summary of the PHP regular expression function

Source: Internet
Author: User
Tags php regular expression

/* Test environment: PHP5.3.29 (PCRE8.32) */

Common functions: (regular expression rules are basically the same as Js_re_read.txt)

Ps:
The pcre in 1.PHP generally uses only these three modifiers: "I" "M" "s".
In a regular expression of 2.PHP, use the note point in the reverse reference:
"/(ABC) \\1/" and '/(ABC) \1/' are equivalent, the former is \\1 because it is enclosed in double quotes (because PHP's double and single quotes are different).

1:int Preg_match (String $pattern, String $subject [, Array & $matches [, int $flags = 0 [, int $offset = 0]])
Parameter description:
$pattern: The pattern to search for, in string form.
$subject: the string being searched for.
$matches: If a parameter matches is supplied, it will be populated with search results. $matches [0] matches the text to the full pattern, $matches [1] matches the first capturing group to the text, and so on.
$flags: The alternate value is Preg_offset_capture, then $matches will become a two-dimensional array, see below.
Offset: Typically, the search starts at the beginning of the target string, and the parameter offset is used to specify a search starting from an unknown target string. (Unit: bytes)

Returns the number of matches, 0 indicates a match failed, 1 indicates a match, an unknown error returns false, the function stops the search after a match, so the match always returns 1, and if you want to perform global mode, use the Preg_match_all () function.

Variable $matched Detailed:
$pattern = "/(\d{2}) (? ') W ' We) \\1 (\w)/";
$subject = "dfg12we12f";
$out =preg_match ($pattern, $subject, $arr);
Print_r ($arr);
>>>array (
[0] = 12we12
[1] = 12
[W] = we
[2] = = We
[3] = f
)

When $flag = Preg_offset_capture, the $matches variable:
$pattern = "/(\d{2}) (We)/";
$subject = "Dfg12we12f55we";
$out =preg_match ($pattern, $subject, $arr, preg_offset_capture);
>>>array (
[0] = = Array (
[0] = 12we
[1] = 3
)
[1] = = Array (
[0] = 12
[1] = 3
)
[2] = = Array (
[0] = = we
[1] = 5
)
)

2:int Preg_match_all (String $pattern, String $subject [, Array & $matches [, int $flags = preg_pattern_order[, int $offse t = 0]])
Parameter description:
$matches: A two-dimensional array.
$flags: (where the first or second can be combined with a third.) )
Preg_pattern_order: Results sorted to $matches[0] saves all matches of the full pattern, $matches [1] saves all matches of the first subgroup, and so on;
Preg_set_order: Results sorted to $matches[0] contains all occurrences of the first match (including subgroups), $matches [1] is an array containing all matches (including subgroups) to the second match, and so on;
Preg_offset_capture: Each found match is returned with an offset to its relative target string.

Returns the number of full matches, or false if no match returns 0, an unknown error occurred.

Variable $matched Detailed:
$pattern = "/(\d{2}) we/";
$subject = "Dfg12we12f55we";
$out =preg_match_all ($pattern, $subject, $arr, Preg_pattern_order);
Print_r ($arr);
>>>array (
[0] = = Array (
[0] = 12we
[1] = 55we
)
[1] = = Array (
[0] = 12
[1] = 55
)
)

If $flag = Preg_set_order:
>>>array (
[0] = = Array (
[0] = 12we
[1] = 12
)
[1] = = Array (
[0] = 55we
[1] = 55
)
)

If $flag = preg_offset_capture: ($flag is also equivalent to Preg_pattern_order + preg_offset_capture)
$pattern = "/(\d{2}) (We)/";
$subject = "Dfg12we12f55we";
$out =preg_match_all ($pattern, $subject, $arr, preg_offset_capture);
>>>array (
[0] = = Array (
[0] = = Array (
[0] = 12we
[1] = 3
)
[1] = = Array (
[0] = 55we
[1] = 10
)
)
[1] = = Array (
[0] = = Array (
[0] = 12
[1] = 3
)
[1] = = Array (
[0] = 55
[1] = 10
)
)
[2] = = Array (
[0] = = Array (
[0] = = we
[1] = 5
)
[1] = = Array (
[0] = = we
[1] = 12
)
)
)

3:string preg_replace (String $pattern, String $replacement, String $subject [, int $limit = -1[, int & $count]]
Parameter description:
$limit: Maximum number of replacements. The default is-1 (No limit).
$count: Returns the number of times the substitution was performed.

Returns a string that has been replaced.

You can use $i (recommended) or \\i to refer to a grouping in $replacement (the range of I is 0~99, and you cannot reference an explicit named grouping).

The above \\i exist in double quotes, and single quotes use \i. (for reasons mentioned above)

If the replacement text requires the use of $ and \:
"\\\\"
"\\$"
‘\\‘
' \$ '

4:string preg_replace_callback (String $pattern, String $callback, String $subject [, int $limit = -1[, int & $count]]
Parameter description:
No

Returns a string that has been replaced.

For example:
$pattern = "/(w) \w/";
$subject = "Welikeappleweyes";
$out =preg_replace_callback ($pattern, "fun1", $subject);
function Fun1 ($m) {
Return "$";
}
Echo $out;
And when using a function to return replacement, both $ and \ \ are invalidated, and the callback function has only one parameter $m, which is the capturing group for this pattern when matching.
Look at the following example: (only modify the above fun1)
function Fun1 ($m) {
Print_r ($m);
}
>>>
Array (
[0] = = we
[1] = W
)
Array (
[0] = = we
[1] = W
)

It is recommended to use anonymous functions directly:
$out =preg_replace_callback ($pattern, function ($m) {
Return "$";
}, $subject);

5:array preg_split (String $pattern, string $subject [, int $limit = -1[, int $flags = 0]])
Parameter description:
$limit: Optional, if specified, the maximum number of substrings to be delimited is limit, and the last substring returned will contain all remaining parts. (limit value is-1 for all separators)
$flag: Alternate, Preg_split_no_empty: Returns the non-empty part after separation.

Returns an array that uses pattern to delimit subject as a boundary.

6:preg_last_error ()
The function has no arguments.
Return: (the error code generated when the match was last executed Pcre)
Preg_no_error-No error occurred
Preg_internal_error-pcre Engine Internal Error
Preg_backtrack_limit_error-Backtracking overrun
Preg_recursion_limit_error-Recursion Overrun
Preg_bad_utf8_error-Abnormal UTF-8 data (occurs only when a regular match is in UTF-8 mode)
Preg_bad_utf8_offset_error-The offset is not matched to a valid UTF-8 character (occurs only when a regular match is in UTF-8 mode)

For example:
Preg_match ('/(?: \ d+|<\d+>) *[!?] /', ' foobar foobar foobar ');
if (preg_last_error () = = Preg_backtrack_limit_error) {
print ' backtracking overrun! ‘;
}

Other:
Other constants:
Pcre_version returns the PCRE version information built into PHP.

Other functions:
Preg_filter ()
Preg_grep ()
Preg_quote ()
Preg_replace_callback_array ()

Summary of the PHP regular expression function

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.