gpt4 book ai didi

javascript - getElementById 给出结果加上字符串

转载 作者:行者123 更新时间:2023-11-28 03:28:07 24 4
gpt4 key购买 nike

我正在尝试使用以下代码从另一个网站提取一个 div:

<?php

$doc = new DomDocument;

//We need to validate our document before refering to the id
$doc->validateOnParse = true;
$doc->loadHtml(file_get_contents('http://myaddresshere'));

var_dump($doc->getElementById('the div'));



?>

我可以得到结果,但在此之前我也得到了一个很长的 php 代码字符串:

object(DOMElement)#2 (18) { ["tagName"]=> string(2) "h3" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "h3" ["nodeValue"]=> string(35) "Telephone Technical Support: Active" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "h3" ["baseURI"]=> NULL ["textContent"]=> string(35) ***"Telephone Technical Support: Active"*** }

我怎样才能删除所有的字符串并只获取 div 的内容。

  • 有问题的 div 显示的是 bool 结果(有效/已过期)

最佳答案

您可以通过两种方式获取节点的值:

DomElement->nodeValue; // inherited from DomNode

或通过

<?php 
function get_inner_html( $node ) {
$innerHTML= '';
$children = $node->childNodes;
foreach ($children as $child) {
$innerHTML .= $child->ownerDocument->saveXML( $child );
}

return $innerHTML;
}
?>

参见 http://php.net/manual/en/class.domelement.phphttp://www.php.net/manual/en/class.domnode.php获取更多信息。

关于javascript - getElementById 给出结果加上字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19813774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com