简体   繁体   English

PHP DomDocument-如何用另一个节点替换节点中的文本

[英]PHP DomDocument - How to replace a text from a Node with another node

I have a node: 我有一个节点:

<p>
    This is a test node with Figure 1.
</p>

My goal is to replace "Figure 1" with a child node: 我的目标是用一个子节点替换“图1”:

<xref>Figure 1</xref>

So that the final result will be: 这样最终结果将是:

<p>
    This is a test node with <xref>Figure 1</xref>.
</p>

Thank you in advance. 先感谢您。

Xpath allows you to fetch the text nodes containing the string from the document. Xpath允许您从文档中获取包含字符串的文本节点。 Then you have to split it into a list of text and element (xref) nodes and insert that nodes before the text node. 然后,您必须将其拆分为文本和元素(xref)节点的列表,并将该节点插入文本节点之前。 Last remove the original text node. 最后删除原始文本节点。

$xml = <<<'XML'
<p>
    This is a test node with Figure 1.
</p>
XML;
$string = 'Figure 1';

$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);

// find text nodes that contain the string
$nodes = $xpath->evaluate('//text()[contains(., "'.$string.'")]');
foreach ($nodes as $node) {
  // explode the text at the string
  $parts = explode($string, $node->nodeValue);
  // add a new text node with the first part
  $node->parentNode->insertBefore(
    $dom->createTextNode(
      // fetch and remove the first part from the list
      array_shift($parts)
    ),
    $node
  );
  // if here are more then one part
  foreach ($parts as $part) {
    // add a xref before it
    $node->parentNode->insertBefore(
      $xref = $dom->createElement('xref'),
      $node
    );
    // with the string that we used to split the text
    $xref->appendChild($dom->createTextNode($string));
    // add the part from the list as new text node
    $node->parentNode->insertBefore(
      $dom->createTextNode($part), 
      $node
    );
  }
  // remove the old text node
  $node->parentNode->removeChild($node);
} 

echo $dom->saveXml($dom->documentElement);

Output: 输出:

<p>
    This is a test node with <xref>Figure 1</xref>.
</p>

You can first use getElementsByTagName() to find the node you're looking for and remove the search text from the nodeValue of that node. 您可以首先使用getElementsByTagName()查找要查找的节点,然后从该节点的nodeValue中删除搜索文本。 Now, create the new node, set the nodeValue as the search text and append the new node to the main node: 现在,创建新节点,将nodeValue设置为搜索文本,并将新节点附加到主节点:

<?php

$dom = new DOMDocument;
$dom->loadHTML('<p>This is a test node with Figure 1</p>');

$searchFor = 'Figure 1';

// replace the searchterm in given paragraph node
$p_node = $dom->getElementsByTagName("p")->item(0);
$p_node->nodeValue = str_replace($searchFor, '', $p_node->nodeValue);

// create the new element
$new_node = $dom->createElement("xref");
$new_node->nodeValue = $searchFor;

// append the child element to paragraph node
$p_node->appendChild($new_node);

echo $dom->saveHTML();

Output: 输出:

<p>This is a test node with <xref>Figure 1</xref></p>

Demo. 演示

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

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