문제

HTML:

<!--a lot of HTML before-->
<div class="quoteheader">
  <div class="topslice_quote"><a href="htsomelink">Some text</a></div>
</div>
<blockquote class="bbc_standard_quote">Some text<br />
</blockquote>
<div class="quotefooter">
  <div class="botslice_quote"></div>
</div>
<br />
<!--a lot of HTML after-->

I NEED TO: remove everything between div.quoteheader and first <br/> after, so result should looks like:

<!--a lot of HTML before-->
<!--a lot of HTML after-->

I TRIED:

$message = preg_replace('/<div\sclass=\"quoteheader\">[^<]+<\/div>/i', '', $string)
도움이 되었습니까?

해결책

I would recommend creating a DomDocument object with the HTML and then using RemoveChild.

다른 팁

You would be much better served used an XML/HTML/DOM parser than regex. SimpleXML is pretty simple.

You would just load up the HTML w/ SimpleXML or some other HTML/XML parser, then use xpath to find the nodes and/or comments you're looking for, then remove them.

An alternative... if you can delimit the code with comments, like this:

<!--code-->
<div> .. </div>
<!--/code-->

you can remove everything between that:

$newstr = preg_replace('/<!--code-->.*?<!--\/code-->/is', '', $htmlstring);
preg_replace('/(\<div\ class="quoteheader"\>)(.+)(<br \/>)/si', '', $string)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top