This article mainly introduces how to learn php regular expressions, learn php regular expressions in detail, and learn more about php regular expressions. if you are interested, refer
Php Regular Expression Learning notes:
1. create a regular expression
$ Regex = '/\ d/I ';
It is a bit like the first method in JavaScript, but here it is a string.
2. special characters in regular expressions
Special characters include:. \ + *? [^] $ () {}=! <> | :-
3. Functions in regular expressions
There are eight 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:
Execute a regular expression match
Returns the matching times of pattern. Its value will be 0 (not matched) or 1 time, because preg_match () will stop searching after the first match.
$subject = "dd133aa2";$pattern = '/\d+/';preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);print_r($matches);
The above sample code adds the parameter "PREG_OFFSET_CAPTURE". In this way, there will be an offset in $ matches. For example"
Preg_match_all:
Execute a global regular expression to match
Returns the number of complete matches (which may be 0), or returns FALSE if an error occurs.
In the following code, $ subject and $ pattern are the same as above. The only difference is that preg_match is replaced with preg_match_all.
preg_match_all($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);print_r($matches);
The number of times returned is 2, matching twice. then, let's look at the input in the array $ matches. There are two. It is one more than above.
Preg_replace:
Search and replace a regular expression
If the subject is an array, preg_replace () returns an array, and otherwise returns a string.
If the match is found, the replaced subject is returned. Otherwise, the unchanged subject is returned. If an error occurs, NULL is returned.
In the code, $ subject is an array containing two strings. Next, replace multiple numbers with uppercase "Z ".
$subject = array("dd133aa2", "kk1ff3");$pattern = '/\d+/';$result = preg_replace($pattern, 'Z', $subject);print_r($result);
Preg_replace_callback:
Execute a regular expression search and use a "callback" to replace
In addition to specifying a callback to replacement for string replacement, this function is equivalent to preg_replace (), including the returned results.
The following code is replaced with the uppercase "Z". the content in $ matches in the callback function is the comments in the code. The first is 133, and the second is 2.
$subject = "dd133aa2";$pattern = '/\d+/';$result = preg_replace_callback($pattern, function($matches) { //$matches [0] => 133 //$matches [0] => 2 return 'Z'; }, $subject);print_r($result);
Preg_grep:
Returns array entries in matching mode.
Use the key in the inputIndexed array.
In the following sample code, I added a "ddsdfd" to the $ subject array, which does not contain numbers. during the matching, filter out the unnumbered ones.
In contrast, $ result2 prints out the filter, but the key is still 2, not 0.
$subject = array("dd133aa2", "kk1ff3", "ddsdfd");$pattern = '/\d+/';$result = preg_grep($pattern, $subject);$result2 = preg_grep($pattern, $subject, PREG_GREP_INVERT);print_r($result);print_r($result2);
Preg_split:
Use a regular expression to separate strings
Returns an array consisting of substrings separated by the pattern boundary.
In the following code, I add brackets to the expression in $ pattern to capture the expression in $ result2.
$subject = "dd133aa2cc";$pattern = '/(\d+)/';$result = preg_split($pattern, $subject);$result2 = preg_split($pattern, $subject, null, PREG_SPLIT_DELIM_CAPTURE);print_r($result);print_r($result2);
Preg_last_error:
Returns the error code generated by the last PCRE regular execution.
Preg_match ('/(? : \ D + | <\ d +>) * [!?] /', 'Foobar foobar foobar'); $ result = preg_last_error (); // The PREG_BACKTRACK_LIMIT_ERROR callback limit exceeds print_r ($ result );
Preg_quote:
Escape regular expression characters
ReturnEscape string.
In the following code, $ subject contains two escape characters: "." and "?".
After $ result is printed, it is "dd \. \? A2cc. in $ result2, a parameter "a" is added. in this case, "a" is also escaped, "dd \. \ \? \ A2cc"
$subject = "dd.a?a2cc";$result = preg_quote($subject);$result2 = preg_quote($subject, 'a');print_r($result);print_r($result2);
4. pattern modifier
The above is a full introduction to php regular expressions, and I hope it will be helpful for your learning.