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

dbcn picture dbcn · Feb 5, 2016 · Viewed 21.8k times · Source

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?

Answer

Pedro Lobito picture Pedro Lobito · Feb 5, 2016

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