Monday 24 September 2018

PHP – Fix “Input is not proper UTF-8, indicate encoding” error when loading xml

When loading xml files in php through simplexml_load_string or domDocument class, sometimes an error like this might popup

Warning: DOMDocument::loadXML(): Input is not proper UTF-8, indicate encoding !
OR
Warning: simplexml_load_string(): Entity: line 93: parser error : Input is not proper UTF-8, indicate encoding !
The error occurs when the xml has some invalid characters that do not fit in the utf-8 character set. The solution to fix this error is quite simple. Just convert the entire xml string to ut8 first and then load.
1
$xml = simplexml_load_string( utf8_encode($rss) );
The utf8_encode function will convert the string to proper utf8 and invalid characters would be fixed, making the xml parseable by simplexml or domdocument.

0 comments:

Post a Comment