PHP DOMDocument,用p包装没有节点的所有元素

时间:2016-02-19 11:04:15

标签: php html class dom domdocument

我从RTE获取HTML。之后我使用DOMDocument类操作它的内容。

编辑有时会给我一个没有节点的文本,例如:

<p>This is some text inside a text-node</p>
This is text without any node and should be wrapped with a text-node

是否可以使用DOMDocument用文本节点包装此文本?

我在函数中使用以下代码:

    $dom = new \DOMDocument();
    $dom->loadHTML($MY_HTML);

    $xpath = new \DOMXPath($dom);

    foreach ($xpath->query('//p') as $k => $paragraph) {
        $paragraph->setAttribute('class', $paragraph->getAttribute('class') . ' bodytext');
    }

    $body = $xpath->query('/html/body');
    return preg_replace('/^<body>|<\/body>$/', '', $dom->saveXml($body->item(0)));

1 个答案:

答案 0 :(得分:2)

该文本在技术上已经在http://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049内,但是这将包含所有包含段落节点的未打包文本节点:

<?php

$html = <<<'END'
<div>
    <p>This is some text inside a text-node</p>
    This is text without any node and should be wrapped with a text-node
</div>
END;

$doc = new \DOMDocument();
$doc->loadHTML($html, LIBXML_HTML_NOIMPLIED);

$xpath = new \DOMXPath($doc);
$nodes = $xpath->query('//text()[not(ancestor::p)][normalize-space()]');

foreach ($nodes as $node) {
    $p = $doc->createElement('p', htmlspecialchars(trim($node->textContent)));
    $node->parentNode->replaceChild($p, $node);
}

print $doc->saveHTML($doc->documentElement);

// <div>
//   <p>This is some text inside a text-node</p>
// <p>This is text without any node and should be wrapped with a text-node</p>
// </div>

关键是使用p XPath查询选择没有//text()[not(ancestor::p)][normalize-space()]祖先的所有非空文本节点。