quoted_printable_decode
(PHP 4, PHP 5, PHP 7, PHP 8)
quoted_printable_decode — 将 quoted-printable 字符串转换为 8-bit 字符串
说明
quoted_printable_decode(string
$str
): string该函数返回 quoted-printable 解码之后的 8-bit 字符串 (参考 » RFC2045 的6.7章节,而不是 » RFC2821 的4.5.2章节,so additional periods are not stripped from the beginning of line)
该函数与 imap_qprint() 函数十分相似,但是该函数不需要依赖 IMAP 模块。
参数
-
str
-
输入的字符串。
返回值
返回的 8-bit 二进制字符串。

User Contributed Notes 21 notes
legolas558 at users dot sausafe dot net ¶
15 years ago
As soletan at toxa dot de reported, that function is very bad and does not provide valid enquoted printable strings. While using it I saw spam agents marking the emails as QP_EXCESS and sometimes the email client did not recognize the header at all; I really lost time :(. This is the new version (we use it in the Drake CMS core) that works seamlessly:
<?php
//L: note $encoding that is uppercase
//L: also your PHP installation must have ctype_alpha, otherwise write it yourself
function quoted_printable_encode($string, $encoding='UTF-8') {
// use this function with headers, not with the email body as it misses word wrapping
$len = strlen($string);
$result = '';
$enc = false;
for($i=0;$i<$len;++$i) {
$c = $string[$i];
if (ctype_alpha($c))
$result.=$c;
else if ($c==' ') {
$result.='_';
$enc = true;
} else {
$result.=sprintf("=%02X", ord($c));
$enc = true;
}
}
//L: so spam agents won't mark your email with QP_EXCESS
if (!$enc) return $string;
return '=?'.$encoding.'?q?'.$result.'?=';
}
I hope it helps ;)
?>
Karora ¶
13 years ago
Taking a bunch of the earlier comments together, you can synthesize a nice short and reasonably efficient quoted_printable_encode function like this:
Note that I put this in my standard library file, so I wrap it in a !function_exists in order that if there is a pre-existing PHP one it will just work and this will evaluate to a noop.
<?php
if ( !function_exists("quoted_printable_encode") ) {
/**
* Process a string to fit the requirements of RFC2045 section 6.7. Note that
* this works, but replaces more characters than the minimum set. For readability
* the spaces aren't encoded as =20 though.
*/
function quoted_printable_encode($string) {
return preg_replace('/[^\r\n]{73}[^=\r\n]{2}/', "$0=\r\n", str_replace("%","=",str_replace("%20"," ",rawurlencode($string))));
}
}
?>
Regards,
Andrew McMillan.
yual at inbox dot ru ¶
8 years ago
I use a hack for this bug:
$str = str_replace("=\r\n", '', quoted_printable_encode($str));
if (strlen($str) > 73) {$str = substr($str,0,74)."=\n".substr($str,74);}
roelof ¶
14 years ago
I modified the below version of legolas558 at users dot sausafe dot net and added a wrapping option.
<?php
/**
* Codeer een String naar zogenaamde 'quoted printable'. Dit type van coderen wordt
* gebruikt om de content van 8 bit e-mail berichten als 7 bits te versturen.
*
* @access public
* @param string $str De String die we coderen
* @param bool $wrap Voeg linebreaks toe na 74 tekens?
* @return string
*/
function quoted_printable_encode($str, $wrap=true)
{
$return = '';
$iL = strlen($str);
for($i=0; $i<$iL; $i++)
{
$char = $str[$i];
if(ctype_print($char) && !ctype_punct($char)) $return .= $char;
else $return .= sprintf('=%02X', ord($char));
}
return ($wrap === true)
? wordwrap($return, 74, " =\n")
: $return;
}
?>
legolas558 ¶
15 years ago
Please note that in the below encode function there is a bug!
<?php
if (($c==0x3d) || ($c>=0x80) || ($c<0x20))
?>
$c should be checked against less or equal to encode spaces!
so the correct code is
<?php
if (($c==0x3d) || ($c>=0x80) || ($c<=0x20))
?>
Fix the code or post this note, please
madmax at express dot ru ¶
21 years ago
Some browser (netscape, for example)
send 8-bit quoted printable text like this:
=C5=DD=A3=D2=C1= =DA
"= =" means continuos word.
php function not detect this situations and translate in string like:
abcde=f
Thomas Pequet / Memotoo.com ¶
15 years ago
If you want a function to do the reverse of "quoted_printable_decode()", follow the link you will find the "quoted_printable_encode()" function:
http://www.memotoo.com/softs/public/PHP/quoted printable_encode.inc.php
Compatible "ENCODING=QUOTED-PRINTABLE"
Example:
quoted_printable_encode(ut8_encode("c'est quand l'été ?"))
-> "c'est quand l'=C3=A9t=C3=A9 ?"
ludwig at gramberg-webdesign dot de ¶
14 years ago
my approach for quoted printable encode using the stream converting abilities
<?php
/**
* @param string $str
* @return string
* */
function quoted_printable_encode($str) {
$fp = fopen('php://temp', 'w+');
stream_filter_append($fp, 'convert.quoted-printable-encode');
fwrite($fp, $str);
fseek($fp, 0);
$result = '';
while(!feof($fp))
$result .= fread($fp, 1024);
fclose($fp);
return $result;
}
?>
soletan at toxa dot de ¶
15 years ago
Be warned! The method below for encoding text does not work as requested by RFC1521!
Consider a line consisting of 75 'A' and a single é (or similar non-ASCII character) ... the method below would encode and return a line of 78 octets, breaking with RFC 1521, 5.1 Rule #5: "The Quoted-Printable encoding REQUIRES that encoded lines be no more than 76 characters long."
Good QP-encoding takes a bit more than this.
steffen dot weber at computerbase dot de ¶
16 years ago
As the two digit hexadecimal representation SHOULD be in uppercase you want to use "=%02X" (uppercase X) instead of "=%02x" as the first argument to sprintf().
pob at medienrecht dot NOSPAM dot org ¶
20 years ago
If you do not have access to imap_* and do not want to use
备份地址:http://www.lvesu.com/blog/php/function.quoted-printable-decode.php