So I'm trying to parse HTML pages and looking for paragraphs (<p>) using get_elements_by_tag_name('p');
The problem is that when I use $element->nodeValue, it's returning weird characters. The document is loaded first into $html using curl then loading it into a DOMDocument.
I'm sure it has to do with charsets.
Here's an example of a response: "aujourd’hui".
Thanks in advance.
I had the same issues and now noticed that loadHTML() no longer takes 2 parameters, so I had to find a different solution. Using the following function in my DOM library, I was able to remove the funky characters from my HTML content.
private static function load_html($html)
{
$doc = new DOMDocument;
$doc->loadHTML('<?xml encoding="UTF-8">' . $html);
foreach ($doc->childNodes as $node)
if ($node->nodeType == XML_PI_NODE)
$doc->removeChild($node);
$doc->encoding = 'UTF-8';
return $doc;
}
Apparently for me none of the above worked, finally I've found the following:
// Create a DOMDocument instance
$doc = new DOMDocument();
// The fix: mb_convert_encoding conversion
$doc->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'));
Source and more info
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