Forum Moderators: coopster
Does anyone know if it's possible to output an HTML document via the DomDocument::saveHTML() function without having the DTD, body, and html tags automatically added?
For example, I have this kind of html:
<form ...>
<div>...</div>
....
</form>
I would like to use DOMDocument to make some changes to this bit of HTML and then insert it into the middle of some HTML. The problem is that once I do this and use saveHTML(), I get:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<body>
<form ...>
<div>...</div>
....
</form>
</body>
</html>
This is obviously a problem if I want to put it in the middle of some other HTML.
I can't feed the file in using loadXML because of the validation errors. It would be great if there was convenient method a variable or method of some sort that could turn this feature off, rather than have to process the string output by saveHTML(), or any other method. Anyone?
<?php
class DOM extends DOMDocument {
public function saveHTML(){
return preg_replace("/(<\/?html>¦<!DOCTYPE.+¦<\/?body>)/", '', parent::saveHTML());
}
}
?>
Now, just call a new DOM() and use it as normal!
However, I would love it if there were a function in the DOMDocument that took care of this!
[edited by: eelixduppy at 12:14 am (utc) on Feb. 2, 2008]
[edit reason] Disabled smileys [/edit]
I must be missing something? If you are the one building the DOM document in the first place, then just don't add the elements ...
<?php
$doc = new DOMDocument();
$form = $doc->createElement('form');
$form = $doc->appendChild($form);
$div = $doc->createElement('div');
$div = $form->appendChild($div);
echo $doc->saveHTML();
?>
<form><div></div></form>
Would be nice if the HTML section of DOMDocument did have an option to omit those automatic tags, at least if your HTML has a root element...
Oh well, if anyone knows of some obscure DOMDocument function/property that does this I'm all ears :)