开发者

php DOMDocument class: return node instance

开发者 https://www.devze.com 2023-02-20 16:29 出处:网络
Using the DOMDocument class, I want to return the div elements as array items $str = \' <div cl开发者_StackOverflow社区ass=\"outer\">outer div text

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

精彩评论

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