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

发布于 2025-01-05 17:08:26 字数 797 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩起发的微风 2025-01-12 17:08:26

试试这个

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

尝试使用使用上述方法编写的文件

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文