PHP DOMDocument,使用p包装所有没有节点的元素 [英] PHP DOMDocument, wrap all elements without node with p
本文介绍了PHP DOMDocument,使用p包装所有没有节点的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从RTE获取HTML。之后,我使用DOMDocument类来处理其内容。
I get HTML from an RTE. I manipulate it's content afterwards with the DOMDocument Class.
编辑器有时会给我文本而没有节点,例如:
The Editor sometimes gives me text without an node, eg.:
<p>This is some text inside a text-node</p>
This is text without any node and should be wrapped with a text-node
包裹
我正在函数内使用以下代码:
I'm using the following code inside a function:
$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)));
推荐答案
从技术上讲,文本已经在文本节点 ,但这会将所有未包装的文本节点与段落节点包装在一起:
The text is technically already inside a "text node", but this will wrap all unwrapped text nodes with paragraph nodes:
<?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 <的非空文本节点/ code>祖先,使用
// text()[not(ancestor :: p)] [normalize-space()]
XPath查询。
The key is to select all the non-empty text nodes without p
ancestors, using the //text()[not(ancestor::p)][normalize-space()]
XPath query.
这篇关于PHP DOMDocument,使用p包装所有没有节点的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文