PHP UTF8 - ISO-8859-1 encoding


PHP UTF8 - ISO-8859-1 encoding

我正在编写我的第一个PHP代码,我想显示一些电子邮件主题。

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>";
...

但对于主题"ääüüöß",输出看起来是这样的:

=?ISO-8859-1?B5OQg/Pwg9vYg3w=?=

关于

解决方案:

我在网上找到了一个非常有用的功能(http://php.net/manual/de/function.imap-mime-header-decode.php),它有两个小语法错误,但经过一点修改,它对我来说很好

最终解决方案看起来是这样的:

//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;
...

我想指出的是,这两个函数是由作者创建的@http://php.net/manual/de/function.imap-mime-header-decode.php我刚刚删除了两个语法错误。

感谢您回复

这是一种常见的邮件格式,称为"可打印报价"。所有非ascii字符都已编码。(请参见http://en.wikipedia.org/wiki/Quoted-printable)

字符串由封装

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

CCD_ 2描述了编码。此处:ISO8859-1

<string>是字符串本身

请使用imap_mime_header_decode()对字符串进行解码(在使用utf8_encode()之前)!

如果Muhammad的答案不够,您可以使用iconv函数来更改字符串的编码

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