如何让 PHP 一般接受 ISO-8859-1 字符?

发布于 2024-11-01 23:13:56 字数 581 浏览 0 评论 0原文

这件事困扰了我很多年,我想一劳永逸地查出真相。我有一个关联数组,其中的字段是使用 ISO-8859-1 字符定义的。例如:

array("utført" => "red");

我还有另一个从文件加载的数组。我已在浏览器中打印该数组,检查 Æ、Ø 和 Å 等值是否完好。我尝试比较这些数组中的两个字段,但我被以下消息震惊了:

未定义索引:第 39 行 utfã¸rt

我忍不住抽泣。每次我在脚本中涉及 UTF-8 之外的任何字母时,它们都会在某个时刻转换为 ã¸r 或类似的废话。

我的脚本文件采用 ISO-8859-1 编码,我从中加载数据的文档是相同的,我尝试将数据保存到的 MySQL 表也是如此。

所以我能得出的唯一结论是 PHP 不接受任何字符集进入其代码,我必须以某种方式强制 PHP 说挪威语。

感谢您的任何建议

仅供参考,我不会接受“只是不要使用这些字符”或“只需在文件加载时用 UTF 等效项替换这些字符”或任何其他黑客解决方案中的任何答案

This has been bugging me for ages and I want to get to the bottom of this once and for all. I have an associative array which fields I have defined using ISO-8859-1 characters. For instance:

array("utført" => "red");

I also have another array that I have loaded in from a file. I have printed this array out in a browser, checking that values like Æ, Ø and Å is intact. I try to compare two fields from these arrays and I'm slapped by the message:

Undefined index: utfã¸rt on line 39

I can't help but sob. Every single damn time I involve any letters outside UTF-8 in a script they are at some point converted into ã¸r or similar nonsense.

My script file is encoded in ISO-8859-1, the document from which I'm loading my data is the same, and so is the MySQL table I'm trying to save the data to.

So the only conclusion I can draw is that PHP isn't accepting just any character-sets into it's code, and I have to somehow force PHP to speak Norwegian.

Thanks for any suggestions

Just FYI, I won't accept any answers in the lines of "Just don't use those characters" or "Just replace those characters with UTF equivalents at file load" or any other hack solutions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

执着的年纪 2024-11-08 23:13:56

当您从外部文件读取数据时,尝试将它们转换为正确的编码。

我脑子里有这样的事情......

  $f = file_get_contents('externaldata.txt');
  $f = mb_convert_encoding($f, 'iso-8859-1');
  // from this point deal with $f whatever you want

另外,看看 mb_convert_encoding( ) 手册以获取更多信息。

When you read your data from external file try to convert them in proper encoding.

Something like this I have on my mind...

  $f = file_get_contents('externaldata.txt');
  $f = mb_convert_encoding($f, 'iso-8859-1');
  // from this point deal with $f whatever you want

Also, look at mb_convert_encoding() manual for more info.

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