views:

30

answers:

1

In addition to my other question about the analyzing of html, and searching for <p> and <ul>/<ol> tags this question.

$in = '<p>Bit\'s of text</p><p>another paragraph</p><ol><li>item1</li><li>item2</li></ol><p>paragraph</p>';

function geefParagrafen($in){
  $dom = new domDocument;
  $dom->loadHTML($in);
  $x = $dom->documentElement;
}

this is how far i got. What is the way to get an $out as an array containing the following:

$out = array('<p>Bit's of text</p><p>another paragraph</p>',
'<p>another paragraph</p>',
'<ol><li>item1</li><li>item2</li></ol>',
'<p>paragraph</p>');

Thanks in advance! I'm really lost in the domdocument structure.

+2  A: 
$in = '<p>Bit\'s of text</p><p>another paragraph</p><ol><li>item1</li><li>item2</li></ol><p>paragraph</p>';


$dom = new domDocument;
$dom->loadHTML($in);

$children = $dom->getElementsByTagName('body')->item(0)->childNodes;

$out = array();

foreach ($children as $child) {
    $out[] = $dom->saveXML($child);
}

print_r($out);
Ionuț G. Stan
Thank you! The $children = $dom->getElementsByTagName('body')->item(0)->childNodes; did the trick. I was walking through $dom with an foreach. But the nodeValue didn't contain the <p> tags.Thank you!
blub
You're welcome.
Ionuț G. Stan