PHP DOM 获取节点值 html?(不带剥离标签) [英] PHP DOM get nodevalue html? (without stripping tags)

查看:28
本文介绍了PHP DOM 获取节点值 html?(不带剥离标签)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 nodeValue 在文件中获取 div 标签的 innerhtml,但是此代码仅输出纯文本并且似乎从 div 内部去除了所有 html 标签.如何更改此代码以输出 div 的 HTML 内容而不是纯文本,并且还输出包装其子元素的主 div.

I am trying to get the innerhtml of div tags in a file using nodeValue, however this code is outputting only plain text and seems to strip out all html tag from inside the div. How can I change this code to output the div's HTML content and not plain text, AND also output the main div wrapping it's child elements.

示例:

file.txt 的内容:

contents of file.txt:

<div class="1"><span class="test">text text text</span></div>
<div class="2"><span class="test">text text text</span></div>
<div class="3"><span class="test">text text text</span></div>

script.php:

script.php:

  $file= file_get_contents('file.txt');

    $doc = new DOMDocument();

    @$doc->loadHTML('<?xml encoding="UTF-8">'.$file); 

    $entries = $doc->getElementsByTagName('div');

        for ($i=0;$i<$entries->length;$i++) {
            $entry = $entries->item($i);
            echo $entry->nodeValue;
        }

输出:文字文字文字文字文字文字文字文字文字

outputs: text text texttext text texttext text text

我需要它输出什么:

<div class="1"><span class="test">text text text</span></div>
<div class="2"><span class="test">text text text</span></div>
<div class="3"><span class="test">text text text</span></div>

注意需要输出父 div (..etc) 以及包装 span 标签...

Notice the parent div's (..etc) are needed to be outputted as well wrapping the span tags...

帮助!

推荐答案

我从来没有做过你想要做的事情,但是作为一个黑暗中的刺,使用 API 文档,确实 echo $entry->textContent;工作?

I have never done what you're attempting to do, but as a stab in the dark, using the API docs, does echo $entry->textContent; work?

添加更新.这是来自 DOMNode 的文档页面上的评论:

Adding an update. This is from the comments located on the docs page for DOMNode:

嗨!

结合所有的注释,获取节点内部HTML的最简单方法是使用这个函数:

Combining all th comments, the easiest way to get inner HTML of the node is to use this function:

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

    return $innerHTML;  }  ?>

或者,也许一个更简单的方法就是这样做:

Or, maybe a simpler method is just to do:

echo $domDocument->saveXML($entry);

这篇关于PHP DOM 获取节点值 html?(不带剥离标签)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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