如何在文本节点的文本中间添加元素?

时间:2016-08-19 13:06:58

标签: php xpath domdocument

给出以下HTML:

$content = '<html>
 <body>
  <div>
   <p>During the interim there shall be nourishment supplied</p>
  </div>
 </body>
</html>';

如何将其更改为以下HTML:

<html>
 <body>
  <div>
   <p>During the <span>interim</span> there shall be nourishment supplied</p>
  </div>
 </body>
</html>

我需要使用DomDocument来做到这一点。以下是我尝试过的内容:

$dom = new DomDocument();
$dom->loadHTML($content);
$dom->preserveWhiteSpace = false;
$xpath = new DOMXpath($dom);
$elements = $xpath->query("//*[contains(text(),'interim')]");
if (!is_null($elements)) {
 foreach ($elements as $element) {
   $text = $element->nodeValue;
   $element->nodeValue = str_replace('interim','<span>interim</span>',$text);
 }
}
echo $dom->saveHTML();

然而,这会输出文字html实体,因此它在浏览器中呈现如下:

During the <span>interim</span> there shall be nourishment supplied

我想应该使用createElementappendChild方法,而不是直接分配nodeValue但我无法看到如何在textNode字符串的中间插入元素?

3 个答案:

答案 0 :(得分:1)

Marcus Harrison使用splitText的答案很好,但它可以简化,需要使用mb_ *方法来处理UTF-8输入:

<?php

$html = <<<END
<html>
<meta charset="utf-8">
<body>
    <div>
        <p>During € the interim there shall be nourishment supplied</p>
    </div>
</body>
</html>
END;

$replace = 'interim';

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

$xpath = new DOMXPath($doc);
$nodes = $xpath->query(sprintf('//text()[contains(., "%s")]', $replace));

foreach ($nodes as $node) { 
    $start = mb_strpos($node->textContent, $replace);
    $end = $start + mb_strlen($replace);

    $node->splitText($end); // do this first
    $node->splitText($start); // do this last

    $newnode = $doc->createElement('span');
    $node->parentNode->insertBefore($newnode, $node->nextSibling);
    $newnode->appendChild($newnode->nextSibling);
}

$doc->encoding = 'UTF-8';

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

答案 1 :(得分:0)

为此,您必须使用DOMString的splitText接口。这接受一个偏移量,可以使用strpos

来检索
$dom = new DomDocument();
$dom->loadHTML($content);
$dom->preserveWhiteSpace = false;
$xpath = new DOMXpath($dom);
$elements = $xpath->query("//*[contains(text(),'interim')]");
if (!is_null($elements)) {
    foreach ($elements as $element) {
        $text = $element->childNodes->item(0);
        $text->splitText(strpos($text->textContent, "interim"));
        $text2 = $element->childNodes->item(1);
        $text2->splitText(strpos($text2->textContent, " "));
        $element->removeChild($text2);
        $span = $dom->createElement("span");
        $span->appendChild($dom->createTextNode("interim"));
        $element->insertBefore($span, $element->childNodes->item(1));
    }
}
echo $dom->saveHTML();

编辑:刚刚测试过它,我意识到我没有删除第二个文本节点中的原始“临时”。编辑这个答案来做到这一点。我还编辑了这段代码,以便与旧版本的PHP兼容,因为我可以想到它:因为我没有运行旧版本的PHP,所以我无法测试它。

答案 2 :(得分:0)

使用修改后的元素创建一个新的DomDocument并替换旧的

 foreach ($elements as $element) {
   $text = $element->nodeValue;

   $el = new DomDocument();
   $el->loadHTML('<iframe>'. str_replace('interim','<span>interim</span>',$text) . '</iframe>');
   $new = $dom->importNode($el->getElementsByTagName('iframe')->item(0), true);
   unset($el);

   $element->parentNode->replaceChild($new, $element);
 }