Understanding the template in preg_match_all () function call

I'm trying to understand how preg_match_all() works, and when I look at the documentation on php.net, I see a few examples, but I am puzzled by the lines sent as a template parameter. Is there really a complete, clear explanation? For example, I do not understand what the image in this example means:

 preg_match_all("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x", "Call 555-1212 or 1-800-555-1212", $phones); 

or that:

 $html = "<b>bold text</b><a href=howdy.html>click me</a>"; preg_match_all("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/", $html, $matches, PREG_SET_ORDER); 

I made an introductory class in PHP but have never seen anything like it. Some clarifications would be appreciated.

Thanks!

+4
source share
4 answers

You are looking for it

Note that the first one is a subset of the second.

+3
source

These are not “PHP patterns,” these are regular expressions. Instead of explaining what has been explained up to a thousand times in this answer, I will point you to http://regular-expressions.info for information and tutorials.

+4
source

Also check out YAPE , which, for example, gives this nice text explanation for your first regular expression:

 (?x-ims:\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?x-ims: group, but do not capture (disregarding whitespace and comments) (case-sensitive) (with ^ and $ matching normally) (with . not matching \n): ---------------------------------------------------------------------- \(? '(' (optional (matching the most amount possible)) ---------------------------------------------------------------------- ( group and capture to \1 (optional (matching the most amount possible)): ---------------------------------------------------------------------- \d{3} digits (0-9) (3 times) ---------------------------------------------------------------------- )? end of \1 (NOTE: because you are using a quantifier on this capture, only the LAST repetition of the captured pattern will be stored in \1) ---------------------------------------------------------------------- \)? ')' (optional (matching the most amount possible)) ---------------------------------------------------------------------- (?(1) if back-reference \1 matched, then: ---------------------------------------------------------------------- [\-\s] any character of: '\-', whitespace (\n, \r, \t, \f, and " ") ---------------------------------------------------------------------- | else: ---------------------------------------------------------------------- succeed ---------------------------------------------------------------------- ) end of conditional on \1 ---------------------------------------------------------------------- \d{3} digits (0-9) (3 times) ---------------------------------------------------------------------- - '-' ---------------------------------------------------------------------- \d{4} digits (0-9) (4 times) ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- 
+2
source

The sample you are writing about is a mini-language in it called Regular Expression. He specialized in finding patterns in strings, swapping, etc. For everything that follows some kind of pattern.

More specifically, it is Perl Compatible Regular Expression (PCRE).

A reference for this language is not available on the PHP manual website, you will find it here: PCRE personal page .

A well-done step-by-step introduction is on the regex website .

+1
source

Source: https://habr.com/ru/post/1389954/


All Articles