简体   繁体   English

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

[英]PHP DOMDocument, wrap all elements without node with p

I get HTML from an RTE. 我从RTE获得HTML。 I manipulate it's content afterwards with the DOMDocument Class. 之后,我将使用DOMDocument类来处理其内容。

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

Is it possible using the DOMDocument to wrap this text with a text-node? 是否可以使用DOMDocument使用文本节点包装此文本?

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: 从技术上讲,文本已经在“ text node”内 ,但这会将所有未包装的文本节点与段落节点包装在一起:

<?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>

The key is to select all the non-empty text nodes without p ancestors, using the //text()[not(ancestor::p)][normalize-space()] XPath query. 关键是使用//text()[not(ancestor::p)][normalize-space()] XPath查询来选择所有没有p祖先的非空文本节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM