在 PHP 中从 JSON 或 XML 格式读取重音字符
我有一个 XML 文件,其中包含像 æøåêèé 这样的重音字符。如果我只是使用 fread 读取文件,我可以轻松读取这些字符,但如果我使用 simplexml_load_string 或 DOMDocument,我将无法读取这些字符。
我的 JSON 文件也是如此,我尝试使用 json_decode
我尝试使用 mb_convert_encoding 并将更改字符更改为 Window-1252、UTF-8 和许多其他编码,但没有任何效果。 我正在使用 PHP 5.3.1。我希望有人帮助编写读取此类字符的演示代码。 以下是我尝试过的基本代码,但它不起作用
XML 文件
<?xml version="1.0" encoding="windows-1252"?>
<note>
<message>Norwegian: æøå. French: êèé</message>
</note>
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'));
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'));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个
尝试使用使用上述方法编写的文件
try this
try working with this file which written using above method