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

PHP DOMDocument,用p包装所有没有节点的元素,php,html,class,dom,domdocument,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用文本节点包装此文本 我在函

我从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)));
$dom=new\DOMDocument();
$dom->loadHTML($MY_HTML);
$xpath=new\DOMXPath($dom);
foreach($xpath->query('//p')为$k=>$paragration){
$paragration->setAttribute('class',$paragration->getAttribute('class')。'bodytext');
}
$body=$xpath->query('/html/body');
返回preg_replace(“/^ |$/”,“$dom->saveXml($body->item(0));

从技术上讲,文本已在中,但这将使用段落节点包装所有未包装的文本节点:

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

从技术上讲,文本已在中,但这将使用段落节点包装所有未包装的文本节点:

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