I currently have a problem reading in XHTML as the XML parser doesn't recognise HTML character entities so:
<?php
$text = <<<EOF
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Entities are Causing Me Problems</title>
</head>
<body>
<p>Copyright © 2010 Some Bloke</p>
</body>
</html>
EOF;
$imp = new DOMImplementation ();
$html5 = $imp->createDocumentType ('html', '', '');
$doc = $imp->createDocument ('http://www.w3.org/1999/xhtml', 'html', $html5);
$doc->loadXML ($text);
header ('Content-Type: application/xhtml+xml; charset: utf-8');
echo $doc->saveXML ();
Results in:
Warning: DOMDocument::loadXML() [domdocument.loadxml]: Entity 'copy' not defined in Entity, line: 8 in testing.php on line 19
How can I fix this while allowing myself to serve pages as XHTML5?
XHTML5 does not have a DTD, so you may not use the old-school HTML named entities in it, as there is no document type definition to tell the parser what the named entities are for this language. (Except for the predefined XML entities <
, &
, "
and >
... and '
, though you generally don't want to use that).
Instead use a numeric character reference (©
) or, better, just a plain unencoded ©
character (in UTF-8; remember to include the <meta>
element to signify the character set to non-XML parsers).
Try using DOMDocument::loadHTML()
instead. It doesn't choke on imperfect markup.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With