简体   繁体   English

解码UTF-8编码头

[英]Decoding UTF-8 Encoded Header

I'm using PHP imap to read emails out of an inbox. 我正在使用PHP imap从收件箱中读取电子邮件。 It extracts some information from headers. 它从标题中提取一些信息。 One of the headers looks like this: 其中一个标题如下所示:

X-My-Custom-Header: =?UTF-8?B?RXVnZW4gQmFiacSH?= X-My-Custom-Header:=?UTF-8?B?RXVnZW4gQmFiacSH?=

The original value of that encoded string is Eugen Babić. 该编码字符串的原始值是EugenBabić。

When I try to decode that string using PHP, I can't get it quite right, the ć always comes back messed up. 当我尝试使用PHP解码该字符串时,我无法理解它,ć总是回来搞砸了。

I've tried imap_utf8, imap_mime_header_decode and a bunch of others I can't quite recall. 我已经尝试了imap_utf8,imap_mime_header_decode以及其他一些我无法回忆的内容。 They either don't return anything at all, or they mess up the ć as I mentioned before. 他们要么根本不归还任何东西,要么就像我之前提到的那样弄乱了ć。

What is the correct way to decode this? 解码这个的正确方法是什么?

imap_utf8 and imap_mime_header_decode work just fine; imap_utf8imap_mime_header_decode工作就好了; there's also iconv_mime_decode : 还有iconv_mime_decode

php > echo imap_utf8('X-My-Custom-Header: =?UTF-8?B?RXVnZW4gQmFiacSH?='), "\n";
X-My-Custom-Header: Eugen Babić

php > list($k,$v) = imap_mime_header_decode('X-My-Custom-Header: =?UTF-8?B?RXVnZW4gQmFiacSH?=');
php > echo $v->text, "\n";
Eugen Babić

php > echo iconv_mime_decode('X-My-Custom-Header: =?UTF-8?B?RXVnZW4gQmFiacSH?=', 0, "utf8"), "\n";
X-My-Custom-Header: Eugen Babić

It seems that imap_utf8 returns its output in NFD, so that the accent over the c may appear out of place in some settings. 似乎imap_utf8在NFD中返回其输出,因此在某些设置中c上的重音可能显得不合适。

Here's what you're doing wrong: You're HTML (as generated by the PHP) is not UTF-8 encoded. 这就是你做错了什么:你的HTML(由PHP生成)不是UTF-8编码的。 So even though it's returning the accented c, the page isn't displaying it correctly. 因此,即使它返回重音c,页面也没有正确显示。

To fix it, add this in your <head> tag: 要解决此问题,请在<head>标记中添加:

<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>

The function mb_decode_mimeheader() solved the problem 函数mb_decode_mimeheader()解决了这个问题

            "fromName" => (isset($fromInfo->personal))
                ? mb_decode_mimeheader( $fromInfo->personal) : "",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM