略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: imap_utf7_encode

2024-04-29

imap_utf7_encode

(PHP 4, PHP 5, PHP 7, PHP 8)

imap_utf7_encodeConverts ISO-8859-1 string to modified UTF-7 text

说明

imap_utf7_encode(string $string): string

Converts string to modified UTF-7 text.

This is needed to encode mailbox names that contain certain characters which are not in range of printable ASCII characters.

参数

string

An ISO-8859-1 string.

返回值

Returns string encoded with the modified UTF-7 encoding as defined in » RFC 2060, section 5.1.3.

参见

add a noteadd a note

User Contributed Notes 5 notes

up
1
dn at dnunes dot com
14 years ago
Maccoy function's isn't reliable 'cause it doesn't follow the RFC 2060. It will work fine for most latin chars (which, in base64, will turn into "+A..."), but for other language it can make a big mess. If you're using an internationalizated system, try using the other presented options.
up
1
marcelo at maccoy dot com dot br
17 years ago
<?php
/**
* @return encoded string
* @param string $str       
* @desc Convert str to UTF-7 imap default mailbox names
    (the imap_utf7_encode don t built the same result)
*/
function getEncodedUtf7($str){
    if(!
$str){
       echo
"error: you need to give a string parameter.";
       return
false;                       
    }
# change spaces by entropy because mb_convert fail with spaces
   
$str=ereg_replace(" ","xyxy",$str);               
# if mb_convert work
   
if($str=mb_convert_encoding($str,"UTF-7")){
       
# change characters
           
$str=preg_replace("/\+A/","&A",$str);
       
# change to spaces again
           
$str=preg_replace("/xyxy/"," ",$str);                       
       
# return encoded string
           
return $str;
# else
   
}else{
       
# print error and return false
           
echo "error: is not possible to encode this string '$str'.[".
           
imap_last_error().
           
"]";
            return
false;                     
    }                
}
?>
up
1
Anonymous
16 years ago
Note that php4 (4.4.0) will encode IMAP folders in a way that no other program will be able to decode them back.
$ php4 <<EOF
> <?php
> echo "ENCODED: " . imap_utf7_encode(") . "\\n";
>
?>
> EOF
ENCODED: &w5w-

Now try to decode the result string ("&w5w-") using e.g. javamail or python's imaplib, or if you don't like these languages - create an IMAP folder using PHP and try to display it in an IMAP client (mutt, thunderbird, outlook, whatever...)
up
0
Olivier
3 years ago
For those who search for a good implementation of modified UTF-7 encoding for IMAP, you can find a code that works on :
http://tananyag.ntszki.hu/mail/functions/imap_utf7_local.php
The result is different from imap-utf7-encode() function and is identical to mailbox name generated by Thunderbird.
up
0
Anonymous
15 years ago
This encoding will not work for cyrus imapservers and you might have problems with other mail clients.
Use mb_convert_encoding($str, "UTF7-IMAP","ISO-8859-15") instead.

ps: mb_convert_encoding() requires   building php with  --enable-mbstring option

官方地址:https://www.php.net/manual/en/function.imap-utf7-encode.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3