【发布时间】:2018-12-28 19:03:20
【问题描述】:
在我更新一些旧项目的过程中,我正在处理一些旧的 ANSI/ASCII 文件和编码。 我想让所有东西都运行 utf-8 以确保我可以支持各种语言。
我有一项服务,我使用微服务发送短信。我有一个端点:/sms.php,我在其中接受一些来自 _GET 的参数,然后在应用程序中使用这些参数。 我有一些测试文件,我在其中提出一些请求以测试一切是否正常。 我的问题是,即使所有文件都是 utf8 编码的(我已经检查了多次)
我的代码如下所示:
$text = "message with æøå to make it utf8";
$params = urlencode($text);
$url = "http://localhost/sms.php?text=".$params;
echo mb_detect_encoding($text, "auto"); // this prints utf8
echo mb_detect_encoding($url, "auto"); // this prints ascii
$res = file_get_contents($url);
这也是我在接收端点看到的。
首先我认为这与 file_get_contents 有关,但由于它在 urlencode 之后被转换,因此它认为我可能就是它。但我不确定如何解决这个问题。 我遇到的另一个问题是我的很多客户都在使用这个旧的 2012 代码(在我开始使用 utf8 作为标准之前),所以我无法更改端点而不导致他们更改当前设置。
在评论中,有人建议我尝试使用以下方法检查字符串是否为 utf8 bin2hex:
bin2hex($_GET['text']); // 6d657373616765207769746820c3a6c3b8c3a520746f206d616b652069742075746638 which is inserted into the database: message with æøå to make it utf8
bin2hex(utf8_decode($_GET['text'])); // 6d657373616765207769746820e6f8e520746f206d616b652069742075746638 which is inserted into the database: message with æøå to make it utf8
希望有人能指出我正确的方向。 例如,我查看了多个 stackoverflow 条目 get utf8 urlencoded characters in another page using php What's the correct encoding of HTTP get request strings?
但我不确定我正在寻找的东西是否可能? 我只是希望能够将整个项目重写为 utf8-ready
谢谢 /嗯
【问题讨论】:
标签: php utf-8 get http-headers ascii