Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

355
Visualizações
PHP DOM: drop XML declaration and add doctype

What I want is to drop XML declaration and add <!DOCTYPE html>. Here is a code example:

$html = new DOMDocument();
$html->formatOutput = true;

$htmlNode = $html->createElement("html");
$html->appendChild($htmlNode);
$htmlNode->appendChild($html->createElement("head"));
$bodyNode = $html->createElement("body");
$bodyNode->setAttribute("data-spy", "scroll");
$bodyNode->setAttribute("data-target", "#myAffix");
$htmlNode->appendChild($bodyNode);
$mainNode = $html->createElement("main");
$bodyNode->appendChild($mainNode);
$gridcellNode = $html->createElement("div");
$gridcellNode->setAttribute("class", "grid-cell");
$mainNode->appendChild($gridcellNode);
$containerFluid = $html->createElement("div");
$containerFluid->setAttribute("class","container-fluid");
$gridcellNode->appendChild($containerFluid);


$html->saveHTMLFile("../article.html");
$html->saveHTML();
$html->save("../article2.html");

The first article.html does not contain XML declaration, but is poor formatted (without indent). The second produced article2.html has indent, but also has <?xml version="1.0"?>. Output in article.html:

<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
<body data-spy="scroll" data-target="#myAffix"><main><div class="grid-cell"><div class="container-fluid"></div></div></main></body>
</html>

output in article2.html:

<?xml version="1.0"?>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  </head>
  <body data-spy="scroll" data-target="#myAffix">
    <main>
      <div class="grid-cell">
        <div class="container-fluid"/>
      </div>
    </main>
  </body>
</html>
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can use LIBXML_NOXMLDECL option to drop the XML declaration but this is only available in Libxml >= 2.6.21, i.e.:

$html->save("../article2.html", LIBXML_NOXMLDECL);

If you don't have Libxml >= 2.6.21, your only option (afaik), is:

$output = $html->saveHTML();
$the_file = "../article2.html";
$html->save($the_file);
file_put_contents($the_file, preg_replace('/<\?xml[^>]+>\s+/', '', file_get_contents($the_file)));
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda