开发者

php DOMDocument class: return node instance

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>';
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜