Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP DOM如何在子项包含标记和文本节点时删除包装标记_Php_Dom - Fatal编程技术网

PHP DOM如何在子项包含标记和文本节点时删除包装标记

PHP DOM如何在子项包含标记和文本节点时删除包装标记,php,dom,Php,Dom,考虑到这个标记 <badtag> This is the title and <em>really</em> needs help <badtag> 我试过以下方法,但效果不太好,我想确保我走在正确的轨道上,不会错过一条更简单的道路。我注意到,当我点击switch语句中的标记(而不是#text)时,我需要添加迭代,以便获得标记的内容(例如使用标记) $l=$origElement->childNodes->length; $new=[]; 对

考虑到这个标记

<badtag>
  This is the title and <em>really</em> needs help
<badtag>
我试过以下方法,但效果不太好,我想确保我走在正确的轨道上,不会错过一条更简单的道路。我注意到,当我点击switch语句中的标记(而不是#text)时,我需要添加迭代,以便获得标记的内容(例如使用标记)

$l=$origElement->childNodes->length;
$new=[];
对于($i=0;$i<$l;++$i){
$child=$origElement->childNodes->item($i);
开关($child->nodeName){
案例“#文本”:
$new[]=$dom->createTextNode($origElement->textContent);
打破
违约:
$new[]=$child;
打破
}
}
foreach($struct的新名称){
$parentNode->insertBefore($struct,$origElement);
}
$Origlement->parentNode->removeChild($Origlement);

我已经创建了一些东西,可以创建要删除的节点内容的克隆。它似乎不喜欢只移动节点,当我使用
cloneNode
时,新版本看起来更干净了

<?php
error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );

$xml = <<<EOB
<DATA>
<badtag>
  This is the title and <em>really</em> needs help
</badtag>
</DATA>
EOB;

$dom = new DOMDocument();
$dom->loadXML($xml);

$origElement = $dom->getElementsByTagName("badtag")[0];
$newParent = $origElement->parentNode;
foreach ( $origElement->childNodes as $child ){
    $newParent->insertBefore($child->cloneNode(true), $origElement);
}
$newParent->removeChild($origElement);
echo $dom->saveXML();

如果你放一个完整的代码示例(函数头以外的完整示例)和1500多行其他与dom和xml相关的转换类(与问题无关),这将很有帮助。我需要删除包装器,但不要丢失发布的代码段中的标记,什么是包装器,什么是标记?还有1500多行其他dom和xml相关的转换类。。。我希望您知道XSLT是什么(专用的XML转换语言),因为您可以减少大量
for
循环和
if
/
切换逻辑。看见当然是为了这个问题!包装器是badtag标签。我不想丢失的标签是其中的em。
      $l = $origElement->childNodes->length;
      $new = [];
      for ($i = 0; $i < $l; ++$i) {
        $child = $origElement->childNodes->item($i);
        switch ($child->nodeName) {
          case '#text':
            $new[] = $dom->createTextNode($origElement->textContent);
            break;
          default:
            $new[] = $child;
            break;
        }
      }
      foreach ($new as $struct) {
        $parentNode->insertBefore($struct, $origElement);
      }
      $origElement->parentNode->removeChild($origElement);
<?php
error_reporting ( E_ALL );
ini_set ( 'display_errors', 1 );

$xml = <<<EOB
<DATA>
<badtag>
  This is the title and <em>really</em> needs help
</badtag>
</DATA>
EOB;

$dom = new DOMDocument();
$dom->loadXML($xml);

$origElement = $dom->getElementsByTagName("badtag")[0];
$newParent = $origElement->parentNode;
foreach ( $origElement->childNodes as $child ){
    $newParent->insertBefore($child->cloneNode(true), $origElement);
}
$newParent->removeChild($origElement);
echo $dom->saveXML();
<?xml version="1.0"?>
<DATA>

  This is the title and <em>really</em> needs help

</DATA>