html - php remove tags before a specified tag -


i want remove image-tags before headline starts, not nested same way. , remove empty tags.

<div class="c2">   <img src="image/file" width="480" height="360" alt="image" /> </div> <div class="c2">   <div class="headline">     headline   </div>   <div class="headline">     headline2   </div> </div> 

and different nested tags like

<div class="c2">   <p>     <img src="image/a.jpg" width="480" height="319" alt="image" />   </p>   <div class="headline">     headline   </div> </div> 

i think solved recursively, dont know how.

thanks help!

edit: if want remove <img> followed <div><div class="headline>" or <div class="headline">, use xpath:

$imgs = $xpath->query("//img[../following-sibling::div[1]/div/@class='headline' or ../following-sibling::div[1]/@class='headline']"); 

see working: http://codepad.viper-7.com/qhprlp

do this:

$doc = new domdocument(); $doc->loadhtml($x); // assuming html in $x $xpath = new domxpath($doc); $imgs = $xpath->query("//img"); // select <img> nodes  foreach ($imgs $img) { // loop through list of <img> nodes $parent = $img->parentnode;  $parent->removechild($img); // delete <img> node if ($parent->childnodes->length >= 1) // if parent node of <img> empty delete         $parent->parentnode->removechild($parent); }  echo htmlentities($doc->savehtml()); // display new html 

see working: http://codepad.viper-7.com/350hw6


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -