Using the DOMDocument class, I want to return the div elements as array items
$str = '
<div cl开发者_StackOverflow社区ass="outer">outer div text
<div class="inner">inner div text</div>
</div>
';
$document = new DOMDocument();
ex:
$item[0] = '<div class="outer">outer div text
<div class="inner">inner div text</div>
</div>'
$item[1] = '<div class="inner">inner div text</div>'
Try this, not perfect code but it works.
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->loadHTML($str);
$entries = ($doc->getElementsByTagName('div'));
foreach($entries as $data) {
$divs[] = '<div>'.trim($data->nodeValue).'</div>';
}
精彩评论