[英]innerHTML in xPath?
有没有办法检索div元素中包含的HTML(和JavaScript)?
I'm not a PHP developer but I found this: 我不是PHP开发人员,但我发现了这个:
function getNodeInnerHTML(DOMNode $oNode)
{
$oDom = new DOMDocument();
foreach($oNode->childNode as $oChild)
{
$oDom->appendChild($oDom->importNode($oChild, true));
}
return $oDom->saveHTML();
}
from http://www.sitepoint.com/forums/showthread.php?p=4225203 来自http://www.sitepoint.com/forums/showthread.php?p=4225203
I don't think you can select content including with only XPath, so a function like the one above may be necessary. 我不认为您可以选择仅包含XPath的内容,因此可能需要像上面那样的功能。 And then you select your div like //div[@id='someID']
etc. 然后你选择你的div像//div[@id='someID']
等。
$xml=new DOMDOCUMENT();
@$xml->loadHTML($htmlcontents);
$xpath=new DOMXPATH($xml);
$nodes=$xpath->query($xpath);
function getHtml($nodes) {
$result = '';
foreach ($nodes as $node) {
$result .= $node->ownerDocument->saveHtml($node);
}
return $result;
}
Source: how to get innerhtml by classname or id using php 来源: 如何使用php通过classname或id获取innerhtml
This will give you the outerHTML rather than the innerHTML, but you can easily strip the extra DIV tags at both ends. 这将为您提供outerHTML而不是innerHTML,但您可以轻松地在两端剥去额外的DIV标记。
Working example: 工作范例:
$xpath = new DOMXPath( @DOMDocument::loadHTML($the_html_code) ) ;
$domElement = $xpath->evaluate("//div[@id='ID_of_the_div']")->item(0) ;
$outerHTML = $domElement->ownerDocument->saveXML($domElement) ;
Now about: 现在关于:
//div[@id='your div'] // div [@ id ='your div']
If you don't care about performance? 如果你不关心表现?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.