I have a problem with the encoding of polish characters using Zend Framework 2. I use:
use Zend\Dom\Query;
Rest code:
$dom = new Query();
$document = '<ul id="test"><li>ęółąśłżźć</li><li>test</li></ul>';
$dom->setDocumentHtml($document);
$dom->setEncoding('utf-8');
$ul = $dom->execute('#test li');
foreach($ul as $li)
{
echo $li->nodeValue;
}
Result:
ÄóÅÄÅÅżźÄtest

How to display correctly these characters?
I tried to use PHP functions (iconv, utf8_encode) but without success.
Under the hood Zend\Dom\Query uses DOMDocument which is built on libxml, whose HTML parser was made for HTML 4. The default encoding for which is ISO-8859-1.
Query::setEncoding() doesn't influence how a document is loaded.
First use mb_convert_encoding to translate anything above the ASCII range into its html entity equivalent.
$dom->setDocumentHtml(mb_convert_encoding($document, 'HTML-ENTITIES', 'UTF-8'));
Or hack in a meta tag or xml declaration specifying UTF-8.
$dom->setDocumentHtml('<meta http-equiv="Content-Type" content="charset=utf-8" />' . $document);
$dom->setDocumentHtml('<?xml encoding="UTF-8">' . $document);
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