PHP 8.5.0 Beta 1 available for testing

Voting

: three minus zero?
(Example: nine)

The Note You're Voting On

developer at nabtron dot com
9 years ago
For those landing here and checking for encoding issue with utf-8 characteres, it's pretty easy to correct it, without adding any additional output tag to your html.

We'll be utilizing: mb_convert_encoding

Thanks to the user who shared: SmartDOMDocument in previous comments, I got the idea of solving it. However I truly wish that he shared the method instead of giving a link.

Anyway coming back to the solution, you can simply use:

<?php

// checks if the content we're receiving isn't empty, to avoid the warning
if ( empty( $content ) ) {
return
false;
}

// converts all special characters to utf-8
$content = mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8');

// creating new document
$doc = new DOMDocument('1.0', 'utf-8');

//turning off some errors
libxml_use_internal_errors(true);

// it loads the content without adding enclosing html/body tags and also the doctype declaration
$doc->LoadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

// do whatever you want to do with this code now

?>

I hope it solves the issue for someone! If you need my help or service to fix your code, you can reach me on nabtron.com or contact me at the email mentioned with this comment.

<< Back to user notes page

To Top