Mysql
 sql >> Base de données >  >> RDS >> Mysql

Comment supprimer des balises dans une balise PHP XML MYSQL GCIDE tout en laissant du texte

Ce code supprime les balises dans la balise cible ():

$str = "<entry><def>some def <altname>hey</altname></def></entry>";

$dom = new domDocument();
$dom -> loadXML($str);

// use getElementsByTagName or use DOMXPath($dom) to find your tag which don't contain other tags
$tags = $dom -> getElementsByTagName("def"); 

$contents = "";
for($i = 0; $tags -> length > $i; $i++){
    $contents = $tags -> item($i) -> nodeValue; //content without tags
    $children = $tags -> item($i) -> childNodes;
    remove_children($tags -> item($i)); //recursively remove chiled nodes
    $tags -> item($i) -> appendChild($dom -> createTextNode($contents));
}

//recursively remove chiled nodes
function remove_children(&$node) {
  while ($node->firstChild) {
    while ($node->firstChild->firstChild) {
      remove_children($node->firstChild);
    }
    $node->removeChild($node->firstChild);
  }
}

echo $dom -> saveXML();