sql >> Database teknologi >  >> RDS >> Mysql

Sådan fjerner du tags i et tag PHP XML MYSQL GCIDE, mens du efterlader tekst

Denne kode strimler tags i target tag ():

$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();



  1. Find noderækkefølge i XML-dokument i SQL Server

  2. Opret indeks i mysql virker ikke

  3. Hvorfor virker mit database backup-script ikke i php?

  4. Forstå MySQL's nye hedebølge