PHP character encoding issue Windows-1252 to UTF-8

Viewed 195

I have a string that has a charset of Windows-1252, it is pulled from a email body using php's imap functions:

$string = 'Great job—and a great decision';

I then try convert it to utf-8 to insert it into our mysql database:

$utf8_encoded = iconv('Windows-1252', "UTF-8//TRANSLIT//IGNORE", $string);

The output comes looking like this:

echo $utf8_encoded;
Great job—and a great decision

It would be great to convert it to utf-8 without the hyphen causing issues. I am not sure what I am doing wrong, any help would be much appreciated, thanks!

I can't always assume the charset will be Windows-1252, I need a solution that will work no matter what charset the email comes in with.

1 Answers

Looks like I was double encoding the string when extracting the email earlier in the code. Problem solved :)

Related