11

我们有几个包含 Windows-1252 字符的数据库字段:

an example pain— if you’re

这些值映射到此列表中的所需值:

http://www.i18nqa.com/debug/utf8-debug.html

我尝试了 htmlentites、mb_detect_encoding、uft8_decode 等的各种排列,但还不能将这些值转换为:

疼痛的例子——如果你是

如何将这些字符转换为它们在 php 中列出的值?

4

2 回答 2

27

您可以使用mb_convert_encoding

$str = "an example pain— if you’re";
$str = mb_convert_encoding($str, "Windows-1252", "UTF-8");
echo $str;
//an example pain— if you’re

演示:http:
//ideone.com/NsIb5x

于 2016-02-05T22:03:31.643 回答
0

哦,天哪,这花了太长时间来解决,所以我想在这里发布我的答案,因为这个链接一直在搜索中出现。我的 MySQL DB 表使用 utf8mb4_unicode_520_ci 进行编码,并且一列有那些烦人的工作大引号。我试图读取数据库值并使用 json_encode 进行编码,但它会失败并且 json_encode 会返回空白,所以我使用了 utf8_encode。那不正确地转换了字符。我不得不使用 mb_convert_encoding 从 Windows-1252 转到 UTF-8,但后来 json_encode 也搞砸了。最后,这奏效了:

$file = urlencode(mb_convert_encoding ($stringwithcurlyquotes, "UTF-8", 'Windows-1252'));

由于我遇到了图像 URL 的问题,因此它运行良好,并且不需要我在另一端对其进行解码。

于 2019-04-09T16:03:59.243 回答