A123321
A123321

Reputation: 837

Convert mail body to UTF-8

I have string "test+=EC=B9=E8=F8=BE=FD=E1=ED=E9+test", it is an email body retrieved using imap_fetchbody(). I want to convert it to proper UTF-8 string "test+ěščřžýáíé+test". I have tried functions imap_utf7_decode, imap_8bit, base64_decode, quoted_printable_decode with no success. Can you please advise me a function which will convert mentioned string to UTF-8?

I am using iconv_mime_decode($str, 0, "UTF-8"); for mail headers, but it does not work for mail body.

Thank you

The answer is in the accepted answer's comments!

Upvotes: 3

Views: 5890

Answers (1)

spencercw
spencercw

Reputation: 3358

Your input string appears to be ISO-8859-2, so you can use this function I have adapted from the comments in the PHP documentation.

function decode_qprint($str) {
    $str = preg_replace("/\=([A-F][A-F0-9])/", "%$1", $str);
    $str = urldecode($str);
    $str = iconv("ISO-8859-2", "UTF-8", $str);
    return $str;
}

Edit: Updated function per the comments:

function decode_qprint($str) {
    $str = quoted_printable_decode($str);
    $str = iconv('ISO-8859-2', 'UTF-8', $str);
    return $str;
}

Upvotes: 4

Related Questions