解析HTML div id包含所有内容 [英] Parse HTML div id include all inner contents

查看:314
本文介绍了解析HTML div id包含所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析一个html文件,并检索div内的所有内容

I am attempting to parse a html file and retreive all the contents within the div

$some_html = file_get_contents($html);   
$dom = new DOMDocument();   
@$dom->loadHtml($some_html);//Strict error checking off     

$xpath = new DOMXPath($dom);      
$result = $xpath->query('//*[@id="IDX-detailsWrapper"]');    
 if (!empty($result)) {    
    foreach ( $result as $link ) {var_dump($link->nodeValue); }} 

html:

<div id="IDX-detailsWrapper"><div id=content><h1>test</h1></div></div>

我希望检索以下字符串

  `<div id=content><h1>test</h1></div>`

而不是

  `test`

目前,它只检索包含在div中的文本,而不是html。

Currently its only retrieving the text contained within the div and not the html.

虽然我不知道这样做的确切做法!

Though im not sure of an exact practice of doing such!

感谢JT

推荐答案

我最终使用了一个库,从 http://sourceforge.net/projects/simplehtmldom/files/

i ended up using a library from http://sourceforge.net/projects/simplehtmldom/files/

并且可以使用的示例代码是

and the sample code that works is

include('simple_html_dom.php');         
$html = file_get_html($html);
foreach($html->find('div#IDX-detailsWrapper') as $e)
echo $e->innertext . '<br>';

希望这有助于某人:)

这篇关于解析HTML div id包含所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆