Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

356
Vistas
PHP DOM: suelte la declaración XML y agregue doctype

Lo que quiero es eliminar la declaración XML y agregar <!DOCTYPE html> . Aquí hay un ejemplo de código:

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

El primer artículo.html no contiene una declaración XML, pero tiene un formato deficiente (sin sangría). El segundo artículo2.html producido tiene sangría, pero también tiene <?xml version="1.0"?> . Salida en 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>

salida en artículo2.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 Respuestas
Responde la pregunta

0

Puede usar la opción LIBXML_NOXMLDECL para descartar la declaración XML, pero esto solo está disponible en Libxml >= 2.6.21 , es decir:

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

Si no tiene Libxml >= 2.6.21 , su única opción (afaik) es:

 $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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda