如何在$ node而不是$ nodeValue中获取html [英] How to get the html inside a $node rather than just the $nodeValue

查看:54
本文介绍了如何在$ node而不是$ nodeValue中获取html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个充满页面的文件夹(页面文件夹),该文件夹中的每个页面(除其他外)都有一个id="short-info"的div.
我有一个代码可以从该文件夹中提取所有<div id="short-info">...</div>并通过使用textContent在其中显示文本(为此目的与nodeValue相同)

I have a folder full of pages (pages-folder), each page inside that folder has (among other things) a div with id="short-info".
I have a code that pulls all the <div id="short-info">...</div> from that folder and displays the text inside it by using textContent (which is for this purpose the same as nodeValue)

加载div的代码:

<?php
$filename = glob("pages-folder/*.php");
sort($filename);
foreach ($filename as $filenamein) {
    $doc = new DOMDocument();
    $doc->loadHTMLFile($filenamein);
    $xpath = new DOMXpath($doc);
    $elements = $xpath->query("*//div[@id='short-info']");

        foreach ($elements as $element) {
            $nodes = $element->childNodes;
            foreach ($nodes as $node) {
                echo $node->textContent;
            }
        }
}
?>

现在的问题是,如果我正在加载的页面有一个子级,例如图像:<div id="short-info"> <img src="picture.jpg"> Hello world </div>,则输出只会是 Hello world ,而不是图像,然后是 Hello世界.

Now the problem is that if the page I am loading has a child, like an image: <div id="short-info"> <img src="picture.jpg"> Hello world </div>, the output will only be Hello world rather than the image and then Hello world.

如何使代码在div id ="short-info"内显示完整的html,例如包括图像而不是文本?

How do I make the code display the full html inside the div id="short-info" including for instance that image rather than just the text?

推荐答案

您必须在节点上进行未记录的调用.

You have to make an undocumented call on the node.

$node->c14n()将为您提供$node中包含的HTML.

$node->c14n() Will give you the HTML contained in $node.

疯狂吧?我在那根头发上掉了一些头发.

Crazy right? I lost some hair over that one.

http://php.net/manual/en/class.domnode.php#88441

更新

这将修改html以符合严格的HTML.最好使用

This will modify the html to conform to strict HTML. It is better to use

$html = $Node->ownerDocument->saveHTML( $Node );

相反.

这篇关于如何在$ node而不是$ nodeValue中获取html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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