fork download
  1. <?php
  2.  
  3. $content = <<<EOD
  4. <div id="foo">
  5. <p>...</p>
  6. </div>
  7.  
  8. <pre class="bar">
  9. <code>
  10. echo "Hello, World!";
  11. </code>
  12. </pre>
  13.  
  14. <p id="third">Hello.</p>
  15.  
  16. Текстовый узел.
  17.  
  18. <table id="fourth">
  19. <tbody>
  20. Content
  21. </tbody>
  22. </table>
  23.  
  24. Ещё один текстовый узел.
  25. EOD;
  26.  
  27. $dom = new DOMDocument( '1.0', 'UTF-8' );
  28. $dom->loadHTML( $content );
  29. $tags = $dom->childNodes->item(1)->childNodes->item(0)->childNodes;
  30. $nodes = [];
  31. for($i=0; $i < $tags->length; $i++ ) {
  32. $node = $tags->item($i);
  33. $nodes[] = utf8_decode($dom->saveHTML($node));
  34. }
  35. var_dump($nodes);
  36.  
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
array(8) {
  [0]=>
  string(33) "<div id="foo">
	<p>...</p>
</div>"
  [1]=>
  string(3) "
 
"
  [2]=>
  string(65) "<pre class="bar">
	<code>
		echo "Hello, World!";
	</code>
</pre>"
  [3]=>
  string(3) "
 
"
  [4]=>
  string(24) "<p id="third">Hello.</p>"
  [5]=>
  string(34) "
 
Текстовый узел.
 
"
  [6]=>
  string(54) "<table id="fourth"><tbody>
		Content
	</tbody></table>"
  [7]=>
  string(47) "
 
Ещё один текстовый узел."
}