Regular expression to match block of HTML

一曲冷凌霜 提交于 2019-12-23 00:49:12

问题


First I'll show you a sample of the code I'm working with:

<div class="entry">
        <p>Any HTML content could go here!</p>
      </div>
    </div><!--/post -->

Normally I'd use a regex rule such as the following to look for a prefix and a suffix and grab everything in between:

(?<=<div class="entry">).*(?=</div><!--/post -->)

However, that doesnt appear to be working as it seems to be pulling the white space in between then following parts instead of the HTML content itself:

<div class="entry">
        <p>

Any help/suggestions would be much appreciated as I've been bashing my head with this one for a good few hours now.

Many thanks in advance.


回答1:


Don't use Regex to parse HTML. You need an Xml Parser or similar.

Search Stackoverflow for the best one, like so: Robust and Mature HTML Parser for PHP




回答2:


You can also consider php strip_tags().



来源:https://stackoverflow.com/questions/5727185/regular-expression-to-match-block-of-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!