13

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?

dbcn
  • 641
  • 1
  • 9
  • 20

2 Answers2

32

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

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

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.

Dave
  • 5,108
  • 16
  • 30
  • 40