从 DOM->getElementById 中检索没有元素 id 的内容
Retrieve content from DOM->getElementById without element id
使用时
$body = $dom->getElementById('content');
输出如下:
<div id=content>
<div>
<p>some text</p>
</div>
</div>
我需要删除 <div id=content></div>
部分。
因为我只需要内部部分,不包括 id content
的 div
需要的结果:
<div>
<p>some text</p>
</div>
我当前的代码:
$url = 'myfile.html';
$file = file_get_contents($url);
$dom = new domDocument;
$dom->loadHTML($file);
//$body = $dom->getElementsByTagName('body')->item(0);
$body = $dom->getElementById('nbscontent');
$stringbody = $dom->saveHTML($body);
echo $stringbody;
getElementById
returns a DOMElement
which has the property childNodes
which is a DOMNodeList
。您可以遍历它以获取子项,然后获取 innerHTML
.
$str = "<div id='test'><p>inside</p></div>";
$dom = new DOMDocument();
$dom->loadHTML($str);
$body = $dom->getElementById('test');
$innerHTML = '';
foreach ($body->childNodes as $child)
{
$innerHTML .= $body->ownerDocument->saveHTML($child);
}
echo $innerHTML; // <p>inside</p>
实例
使用时
$body = $dom->getElementById('content');
输出如下:
<div id=content>
<div>
<p>some text</p>
</div>
</div>
我需要删除 <div id=content></div>
部分。
因为我只需要内部部分,不包括 id content
需要的结果:
<div>
<p>some text</p>
</div>
我当前的代码:
$url = 'myfile.html';
$file = file_get_contents($url);
$dom = new domDocument;
$dom->loadHTML($file);
//$body = $dom->getElementsByTagName('body')->item(0);
$body = $dom->getElementById('nbscontent');
$stringbody = $dom->saveHTML($body);
echo $stringbody;
getElementById
returns a DOMElement
which has the property childNodes
which is a DOMNodeList
。您可以遍历它以获取子项,然后获取 innerHTML
.
$str = "<div id='test'><p>inside</p></div>";
$dom = new DOMDocument();
$dom->loadHTML($str);
$body = $dom->getElementById('test');
$innerHTML = '';
foreach ($body->childNodes as $child)
{
$innerHTML .= $body->ownerDocument->saveHTML($child);
}
echo $innerHTML; // <p>inside</p>
实例