Jump to content

DomDocument saveHTML closing tags


mcastles

Recommended Posts

Hi,

 

I've been using DomDocument in PHP5 and its been working really well. I just have a few questions about it:

 

I'm loading a html file using:

$dom = new DomDocument();
@$dom->loadHTMLFile("html.html");

then I'm modifying the domdocument and then save the html using:

echo $dom->saveHTML();

 

This all works fine and I'm fairly happy with it. The only issue I have is that the new output generates invalide html code. Basically link tags and meta tags don't have closing tags anymore. For example..

 

The original html file might have some link tags like this:

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link rel="stylesheet" type="text/css" href="default.css" media="screen" />
<link rel="stylesheet" type="text/css" href="print.css" media="print" />

 

but after loading and saving with DomDocument it outputs like this:

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="default.css" media="screen">
<link rel="stylesheet" type="text/css" href="print.css" media="print">

 

Has anyone come accross this and know a workaround?

 

Oh, and one other thing:

 

does $dom->formatOutput = true; and $dom->preserveWhiteSpace = false; do anything for loaded html files? I can't seem to get it to modify the output at all.

Link to comment
https://forums.phpfreaks.com/topic/39884-domdocument-savehtml-closing-tags/
Share on other sites

For anyone interested... I have come up with a workaround to convert link, meta and break return tags to have closing slashes.

 

$search = array('/<link(.*)>/i','/<meta(.*)>/i','/<br(.*)>/i');
$replace = array('<link$1/>','<meta$1/>','<br$1/>');
$html = preg_replace($search,$replace, $html);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.