Do you have a question? Post it now! No Registration Necessary. Now with pictures!
December 4, 2005, 11:11 pm
rate this thread
DOMDocument constructor doesn't quite cut it because you need
DOMImplementation to create a DOCTYPE.
Here is how everyone sane would do it:
$doctype = DOMImplementation::createDocumentType(
,'-//W3C//DTD XHTML 1.0 Strict//EN'
$doc = DOMImplementation::createDocument(null, 'html', $doctype);
$doc->formatOutput = true;
$doc->encoding = 'UTF-8';
$head = $doc->createElement('head');
$body = $doc->createElement('body');
OK, so now we have HEAD and BODY inside HTML. So far so good. But
here's the strange part: on output (saveXML() or saveHTML) a META tag
appears inside the HEAD:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Obviously we haven't created this META tag. So who did? Where did it
come from? I didn't want text/html, I want application/xhtml+xml, so I
tried to explicitly override it:
$meta = $this->doc->createElement('meta');
$meta->setAttribute('content', 'application/xhtml+xml; charset='.$enc);
No use. On every output my META Content-Type tag gets overriden by the
generated META tag.
Manipulating DOM with PHP5 is poorly documented and not much discussed,
which probably means a few people use it. I hope someone can clear out
this oddity for me for it looks like a feature, not a bug.
I wouldn't like to believe that the only solution to change the
content-type is str_replace() after outputting to a string :(
- » php extensions and windows... specifically extension_dir
- — Previous thread in » PHP Scripting Forum