How to convert Windows-1252 characters to values in php?

两盒软妹~` 提交于 2019-12-19 07:21:51

问题


We have several database fields that contain Windows-1252 characters:

an example pain— if you’re

Those values map to the desired values from this list:

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

I've tried various permutations of htmlentites, mb_detect_encoding, uft8_decode, etc, but have not yet been able to transform those values to:

an example pain — if you're

How can I transform these characters to their listed values in php?


回答1:


You can use 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

DEMO:
http://ideone.com/NsIb5x




回答2:


Oh my god, this took too long to solve so I want to post my answer here since this link kept coming up in searches. My MySQL DB table has encoding with utf8mb4_unicode_520_ci and a column has those annoying work curly quotes. I was trying to read the DB value and encode with json_encode but it would fail and json_encode would return blank so I used utf8_encode. That improperly converted the character. I had to use mb_convert_encoding to go from Windows-1252 to UTF-8 but then the json_encode messed that up too. In the end, this worked:

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

Since I was having the issue with an image URL, this worked perfectly and didn't require me to decode it on the other side.



来源:https://stackoverflow.com/questions/35234129/how-to-convert-windows-1252-characters-to-values-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!