Quick n Dirty
Quick n Dirty

Reputation: 569

PHP UTF8 - ISO-8859-1 encoding

I am working on my first PHP code and i want to display some email subjects.

header('content-type: text/html; charset=utf-8');
...
$header = imap_headerinfo($imap, $i);
$raw_body = imap_body($imap, $i);
$subject = utf8_encode($header->subject);

echo $subject;
echo "<br>";
...

But for the Subject "ää üü öö ß" the output looks like that:

=?ISO-8859-1?B?5OQg/Pwg9vYg3w==?=

regards

Solution:

I found a very helpful function on the web (http://php.net/manual/de/function.imap-mime-header-decode.php), it has two little syntax errors but after a little rework it worked fine for me.

In the End the Solution looks like that:

//return supported encodings in lowercase.
function mb_list_lowerencodings() { $r=mb_list_encodings();
  for ($n=sizeOf($r); $n--; ) { $r[$n]=strtolower($r[$n]); } return $r;
}

//  Receive a string with a mail header and returns it
// decoded to a specified charset.
// If the charset specified into a piece of text from header
// isn't supported by "mb", the "fallbackCharset" will be
// used to try to decode it.
function decodeMimeString($mimeStr, $inputCharset='utf-8',     
$targetCharset='utf-8',$fallbackCharset='iso-8859-1') {
$encodings=mb_list_lowerencodings();
$inputCharset=strtolower($inputCharset);
$targetCharset=strtolower($targetCharset);
$fallbackCharset=strtolower($fallbackCharset);

$decodedStr='';
$mimeStrs=imap_mime_header_decode($mimeStr);
for ($n=sizeOf($mimeStrs), $i=0; $i<$n; $i++) {
  $mimeStr=$mimeStrs[$i];
  $mimeStr->charset=strtolower($mimeStr->charset);
if (($mimeStr == 'default' && $inputCharset == $targetCharset)
  || $mimeStr->charset == $targetCharset) {
  $decodedStr.=$mimStr->text;
} else {
  $decodedStr.=mb_convert_encoding(
    $mimeStr->text, $targetCharset,
    (in_array($mimeStr->charset, $encodings) ?
      $mimeStr->charset : $fallbackCharset)

  );
}
} return $decodedStr;
}

...

$header = imap_headerinfo($imap, $i);
$raw_body = imap_body($imap, $i);
$sub = decodeMimeString($header->subject);
echo $sub;
...

I want to point out, that the two functions are created by the author @http://php.net/manual/de/function.imap-mime-header-decode.php and I just removed two syntax errors.

Thank you for you replies

Upvotes: 2

Views: 10572

Answers (2)

freytag
freytag

Reputation: 4819

This is a common format for mails, called "quoted printable". All non ascii characters are encoded. (See http://en.wikipedia.org/wiki/Quoted-printable)

The string is encapsulated by

=?<encoding>?Q?<string>?=

<encoding> describes the encoding. Here: ISO8859-1

<string> is the string itself

Please use imap_mime_header_decode() to decode the string ( before using utf8_encode() )!

Upvotes: 1

cyborg86pl
cyborg86pl

Reputation: 2617

If Muhammad's answer won't be enough, you can use iconv function that allows you to change encoding of string

iconv("ISO-8859-1", "UTF-8", $your_string);

Upvotes: 0

Related Questions