fork download
  1. <?php
  2. function merge_html($html) {
  3. $dom = new DOMDocument;
  4. $dom->loadHTML("<div id=\"tmptmptmp\">" . $html . "</div>", LIBXML_HTML_NOIMPLIED|LIBXML_HTML_NODEFDTD);
  5. $xp = new DOMXPath($dom);
  6. $links = $xp->query('//div');
  7. foreach ($links as $link) {
  8. $class = $link->getAttribute('class');
  9. $nested = $xp->query('.//div', $link);
  10. foreach ($nested as $n) {
  11. if ($n->getAttribute('class') == $class) {
  12. //$html = $doc->getElementsByTagName("html")->item(0);
  13. $fragment = $dom->createDocumentFragment();
  14. while ($n->childNodes->length > 0) {
  15. $fragment->appendChild($n->childNodes->item(0));
  16. }
  17. $n->parentNode->replaceChild($fragment, $n);
  18. }
  19. }
  20. }
  21. return preg_replace('/^\s*<div\s+id="tmptmptmp">\s*|\s*<\/div>\s*$/', '', $dom->saveHTML());
  22. }
  23. $html = '
  24. <div class="dotted-highlight">
  25. Here is a check list to determine whether you want this career or not.
  26. <div class="dotted-highlight">
  27. Tick all those apply to you. The more boxes you tick, the better suited you are to this career.
  28. <ul>
  29. <li>
  30. You are hyper active and are always on your toes.
  31. </li>
  32. <li>
  33. You are mentally and physically strong enough to withstand rigorous exercise.
  34. </li>
  35. </ul>
  36. </div>
  37. <div class="dotted-highlight">
  38. You do not mind working in the early morning, late evenings, and on weekends and holidays.
  39. </div>
  40. </div>
  41. <h2>
  42. What work does it involve?
  43. </h2><strong>To become a fitness instructor or a personal trainer, your main job is to help people with their exercise routines.</strong>
  44. ';
  45. $html = merge_html($html);
  46. var_dump($html);
  47. ?>
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
string(801) "<div class="dotted-highlight">
	      Here is a check list to determine whether you want this career or not.
	      
	        Tick all those apply to you. The more boxes you tick, the better suited you are to this career.
	        <ul>
	          <li>
	            You are hyper active and are always on your toes.
	          </li>
	          <li>
	            You are mentally and physically strong enough to withstand rigorous exercise.
	          </li>
	        </ul>
	      
	      
	        You do not mind working in the early morning, late evenings, and on weekends and holidays.
	      
	    </div>
	          <h2>
        What work does it involve?
      </h2><strong>To become a fitness instructor or a personal trainer, your main job is to help people with their exercise routines.</strong>"