0
votes

Im generating a XML file from database that is formated to utf-8 and creating a XML file, however for a some specific case it is not converting properly and displaying me this message :

DOMDocument::loadXML(): Input is not proper UTF-8, indicate encoding ! Bytes: 0x96 0x20 0x50 0x61 in Entity, line: 1

I have already tried all possible online solutions, going from iconv , trying to do regex but none of these are solving the problem. The mb_encoding returns it is ASCII , which is supposedly UTF-8, even checking the file itself its utf-8.

This is my file start which loads the file path from the database which is the variable $xml_file, all inputs from database are being decoded using utf8_decode.

<?php
$content = utf8_encode(file_get_contents($xml_file));
//$encoding = mb_detect_encoding($content);
//$myXMLString = file_put_contents($xml_file, iconv('WINDOWS-1251', 'UTF-8', file_get_contents($xml_file)));
$xml_doc = new DomDocument();
$xml_doc->formatOutput = true;
$xml_doc->preserveWhiteSpace = false;
$xml_doc->loadXML($content);
?>

This is only happening with some items because other generate correctly, however i can not find any particular difference between them neither a permanent fix for this.

HOW I FIXED :

$content = iconv('UTF-8', 'UTF-8//IGNORE', $content);

1
Can you share the original file? - miken32
@miken32 Posted it just now bud, looking foward to have your help - Goncalo
I meant in your code you've got file_get_contents($xml_file). What does that look like, before you've done anything else with it? - miken32
@miken32 Check the file that creates the xml in the original post, it first creates a XML file , then i save that XML path in database and i pull it with file_get_contents to add articles to it. - Goncalo
I fixed it ! Added this before loading the XML $content = iconv('UTF-8', 'UTF-8//IGNORE', $content); - Goncalo

1 Answers

0
votes

Managed to fix this converting it again to UTF-8:

$content = iconv('UTF-8', 'UTF-8//IGNORE', $content);