John Davidson

PHP character encoding issue Windows-1252 to UTF-8

0 comments
Message:


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.



My Answer:
One possible solution is to use mb_convert_encoding instead of iconv. This function can automatically detect the input encoding and convert it to UTF-8. Here's how you can modify your code:

php
$string = 'Great job—and a great decision';
$utf8_encoded = mb_convert_encoding($string, 'UTF-8', mb_detect_encoding($string));

echo $utf8_encoded;


This should correctly convert the string to UTF-8 without any issues with special characters. Let me know if this works for you!

Rate this post

3 of 5 based on 8083 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us