简体   繁体   English

IMAP编码和解码:UTF-8问题

[英]IMAP Encoding and decoding: UTF-8 issue

I have a function which is meant to move a mail from one folder to another on a gmail account. 我有一个功能,旨在将Gmail帐户中的邮件从一个文件夹移动到另一个文件夹。 The function is fully functional when it comes to moving the mail. 在移动邮件时,该功能是完全可用的。 Tho my problem appears when working with utf-8 encoded mailboxes. 使用utf-8编码的邮箱时出现我的问题。 I decode the IMAP folder list response but the dump of both values gives different results. 我解码了IMAP文件夹列表响应,但是两个值的转储给出了不同的结果。

// Getting the folders
$folders = imap_list(CONNECTION, MAILBOX, PATTERN);

// After a foreach, stripping slash, prefix and such
// $folder is the raw mailbox name from the IMAP list
$mailbox = utf8_encode(imap_utf7_decode($folder)); // = string(12) "Tæstbåks"

// The entered search from the client
$search_for = "Tæstbåks"; // = string(10) "Tæstbåks"

if($search_for == $mailbox)
    print "Yeah!";
else
    print "Noo!";

I do not know why those two strings do not match, that is my problem. 我不知道为什么这两个字符串不匹配,这是我的问题。

PHP's function imap_utf7_decode($folder) is documented to return a string in ISO-8859-1 encoding. PHP的函数imap_utf7_decode($folder) 记录为以ISO-8859-1编码返回字符串。 Given that IMAP's modified UTF-7 scheme can encode the whole range of Unicode (which means "a lot") and that ISO-8859-1 can only represent 256 individual characters, you cannot possibly use that function in this context. 鉴于IMAP的修改后的UTF-7方案可以对Unicode的整个范围进行编码(这意味着“很多”),并且ISO-8859-1只能表示256个单独的字符,因此您不可能在这种情况下使用该功能。 I would go as far as to suggest that the PHP developer who decided to offer such a useless function was not in his best shape the day he designed it. 我要建议的是,决定提供这种无用功能的PHP开发人员在他设计该产品的那一天并没有处于最佳状态。

It looks like the mbstring extension can do what you really want to do here -- use something like $mailbox = mb_convert_encoding($folder, "UTF-8", "UTF7-IMAP") , as suggested in the comments below the PHP's docs. 看起来mbstring扩展可以完成您真正想做的事情-使用类似$mailbox = mb_convert_encoding($folder, "UTF-8", "UTF7-IMAP") ,如PHP文档下面的注释中所建议。

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

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