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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.