regex problem in php

后端 未结 5 1524
情书的邮戳
情书的邮戳 2021-01-26 17:45
...

How to match the html inside(including)

in PHP?

I need a

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-26 18:10

    Here is your Regex:

    preg_match('/
    .*<\/div>/simU', $string, $matches);

    But:

    • RegEx do not know what XML/HTML elements are. To them, HTML is just a string. This is why the others are right. Regex are not for parsing a DOM. They are used to find string patterns.
    • I have provided the Regex because you do not intend to parse an entire HTML page, but just grab one defined piece of text from it, in which case a Regex is fine to use.
    • If there is a nested DIV inside the DIV, the Regex will not work as expected. If this is the case, do not use Regex. Use one of the other solutions, because then you need DOM parsing, not string matching.
    • For finding strings with a more or less clearly defined start and end, consider using regular string functions instead, as they are often quicker.

提交回复
热议问题