Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用GetElementsByTagName时,有没有办法保留html标记?_Php_Domdocument - Fatal编程技术网

Php 使用GetElementsByTagName时,有没有办法保留html标记?

Php 使用GetElementsByTagName时,有没有办法保留html标记?,php,domdocument,Php,Domdocument,我有以下代码: $url = file_get_contents('url'); $webpage = new DOMDocument(); $webpage->loadHTML($url); $tables = $webpage->getElementsByTagName('table'); echo $tables->item(3)->nodeValue; 当我回显该方法从网页获得的第三个表时,我可以看到没有html标记,但我需要它们,因为标记包含我在获取元素后处理

我有以下代码:

$url = file_get_contents('url');
$webpage = new DOMDocument();
$webpage->loadHTML($url);
$tables = $webpage->getElementsByTagName('table');
echo $tables->item(3)->nodeValue;
当我回显该方法从网页获得的第三个表时,我可以看到没有html标记,但我需要它们,因为
标记包含我在获取元素后处理的信息


我一直在从php.net中搜索解决方案,可能是NodeList类的某些属性,但我什么都没有。

这里有一个函数,可以用来获取节点的html内容:

function innerHTML(DOMNode $node)
{
    $doc = new DOMDocument();
    foreach ($node->childNodes as $child) {
        $doc->appendChild($doc->importNode($child, true));
    }

    return $doc->saveHTML();
}

DOMNode-DOMNodelist::item(int$index)
需要索引作为参数。索引以0开头。所以第三个应该是索引2


例如,您可以使用此方法递归获取内容。

尝试解决方案?HTML标记?nvm,我明白了。您正在使用
nodeValue
就像一个
innerHTML
调用。很抱歉,可能是第四个表的重复,索引3。