email中header的内容如果不是ascii码,就会编码成mime类型的信息。在php中应该如何对这些信息解码?不知道为什么用iconv_mime_decode有些情况下不能正常解码。另外,发现有header中的内容可能有多段mime的编码的内容(=?.+?=)。请教用php是否有标准的解析mime header的方法?
回复内容:
email中header的内容如果不是ascii码,就会编码成mime类型的信息。在php中应该如何对这些信息解码?不知道为什么用iconv_mime_decode有些情况下不能正常解码。另外,发现有header中的内容可能有多段mime的编码的内容(=?.+?=)。请教用php是否有标准的解析mime header的方法?
标准的解析方法... 内置函数应该是没有...
但是有个第三方库你可以使用下...
php-mime-mail-parser
写了个解析方法,测试基本通过。function parse_mime($pending, $to_charset = 'UTF-8') {
$start = strpos($pending, '=?');
if ($start === false) {
return $pending;
}
$end = strpos($pending, '?=', $start + 2);
if ($end === false) {
return $pending;
}
while ($start !== false && $end !== false) {
$segment = substr($pending, $start, $end - $start + 2);
$charset_end = strpos($segment, '?', 2);
$charset = substr($segment, 2, $charset_end - 2);
$encType = strtoupper($segment{$charset_end + 1});
$encoded_text = substr($segment, $charset_end + 3, strlen($segment) - $charset_end -
5);
switch ($encType) {
case 'Q':
$encoded_text = str_replace('_', ' ', $encoded_text);
$text = quoted_printable_decode($encoded_text);
break;
case 'B':
$text = base64_decode($encoded_text);
break;
}
$text = mb_convert_encoding($text, $to_charset, $charset);
$pending = str_replace($segment, $text, $pending);
// next encoded-word.
$start = strpos($pending, '=?');
$end = strpos($pending, '?=', $start + 2);
}
return $pending;
}
本文原创发布php中文网,转载请注明出处,感谢您的尊重!