How does php match HTML tags
This article mainly explains "how php matches HTML tags". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how php matches HTML tags.
1. Just want to match the characters "?"
Need to escape, that is,\?
2. For non-greedy matching
That is, the recent match:
[greedy] pattern: a.Secretc string: abcabc match result: abcabc [non-greedy] pattern: a. Greedy c string: abcabc match result: abc
Commonly used:
(1) *? Repeat any number of times, but as few as possible
(2) +? Repeat one or more times, but as little as possible
(3)? Repeat 0 or 1 times, but as little as possible
(4) {n ~ (th) m}? Repeat n to m times, but as little as possible
(5) {n,}? Repeat more than n times, but as little as possible
3. Do not capture mode?
It means to add a?: to the data you don't want to capture with (), for example:
Pattern: (?: aaa) (bbb) match result: $1=bbb
Aaa will not be captured by $1
1. [.\ n] *?
Not at all... Cause unknown
2. *?
Yes, you need to add / s at the end, review:
(1) I is not case sensitive
(2) in s mode. Matches all characters, including newline characters
3. (. |\ n) *?
It feels OK, but for very long strings, such as the content of the article "three Kingdoms Advanced Auxiliary _ v1.0", it is not good. It ends before the match is finished, resulting in the article cannot be displayed.
According to the "summary of questions about regular matching of arbitrary characters including newline characters in php", it has something to do with the version of the PCRE library bound by php, but I use php7.0.
4. [\ s\ S] *?
The best match, here.
(1)\ s matches any white space character, including newline characters, which is equivalent to [\ f\ n\ r\ t\ v]
(2)\ S matches any non-white space character, which is equivalent to ^ [\ f\ n\ r\ t\ v]
Together, it matches all the characters.
Need to match, as long as the text does not appear this label, you can match like this:
/ ([\ s\ S] *?) / I at this point, I believe you have a better understanding of "how php matches HTML tags". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!