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

末鹿安然 提交于 2019-12-01 18:24:42

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

thedoctore

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.

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