简体   繁体   English

在PHP中以JSON或XML格式读取重音字符

[英]Read Accented Characters from JSON or XML format in PHP

I have a XML file which have accented characters like æøåêèé. 我有一个带有重音符(如æøåêèé)的XML文件。 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. 如果仅使用fread读取文件,则可以轻松读取这些字符,但是如果使用simplexml_load_string或DOMDocument,则无法读取这些字符。

Same is the case with my JSON file where I tried using json_decode 我尝试使用json_decode的JSON文件也是如此

I have tried using mb_convert_encoding and changing changing characters to Window-1252,UTF-8 and many other encoding but nothing work. 我尝试使用mb_convert_encoding并将更改的字符更改为Window-1252,UTF-8和许多其他编码,但没有任何效果。 I am using PHP 5.3.1. 我正在使用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文件

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

PHP Code PHP代码

   $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'));

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 尝试使用使用上述方法编写的文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM