Gunjan Nigam
Gunjan Nigam

Reputation: 1393

Read Accented Characters from JSON or XML format in PHP

I have a XML file which have accented characters like æøåêèé. If I simply read the file using fread I can read these characters easily but if I use simplexml_load_string or DOMDocument I am not able to read these characters.

Same is the case with my JSON file where I tried using json_decode

I have tried using mb_convert_encoding and changing changing characters to Window-1252,UTF-8 and many other encoding but nothing work. I am using PHP 5.3.1. I would like somebody to help with a demo code of reading such charcaters. Following is my basic code which I tried but it didn't worked

XML File

    <?xml version="1.0" encoding="windows-1252"?>
    <note>
    <message>Norwegian: æøå. French: êèé</message>
    </note>

PHP Code

   $myFile = "check.xml";
   $fh = fopen($myFile, 'r');
   $theData = fread($fh, filesize($myFile));
   fclose($fh);
   echo $theData."<br>";
   $xml = simplexml_load_string($theData);
   print_r(mb_convert_encoding($xml->message,'Windows-1252'));

Upvotes: 2

Views: 772

Answers (1)

maxjackie
maxjackie

Reputation: 23322

try this

$fc = iconv('windows-1250', 'utf-8', file_get_contents(check.xml));
$handle=fopen("abc.xml", "rw");
fwrite($handle, $fc);
fclose($handle);

try working with this file which written using above method

Upvotes: 1

Related Questions