Prep Quoted Printable
Prepares string for Quoted-Printable Content-Transfer-Encoding Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
string Core_Email::_prep_quoted_printable( string $str [, int $charlim = empty ] )
参数列表
参数 类型 描述 默认值 $str
string
$charlim
int
empty
string
protected function _prep_quoted_printable($str, $charlim = '')
{
// Set the character limit
// Don't allow over 76, as that will make servers and MUAs barf
// all over quoted-printable data
if ($charlim === '' || $charlim > 76)
{
$charlim = 76;
}
// Reduce multiple spaces & remove nulls
$str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
// Standardize newlines
if (strpos($str, "\r") !== false)
{
$str = str_replace(array("\r\n", "\r"), "\n", $str);
}
// We are intentionally wrapping so mail servers will encode characters
// properly and MUAs will behave, so {unwrap} must go!
$str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
$escape = '=';
$output = '';
foreach (explode("\n", $str) as $line)
{
$length = strlen($line);
$temp = '';
// Loop through each character in the line to add soft-wrap
// characters at the end of a line " =\r\n" and add the newly
// processed line(s) to the output (see comment on $crlf class property)
for ($i = 0; $i < $length; $i++)
{
// Grab the next character
$char = $line[$i];
$ascii = ord($char);
// Convert spaces and tabs but only if it's the end of the line
if ($i === ($length - 1) && ($ascii === 32 || $ascii === 9))
{
$char = $escape.sprintf('%02s', dechex($ascii));
}
elseif ($ascii === 61) // encode = signs
{
$char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
}
// If we're at the character limit, add the line to the output,
// reset our temp variable, and keep on chuggin'
if ((strlen($temp) + strlen($char)) >= $charlim)
{
$output .= $temp.$escape.$this->crlf;
$temp = '';
}
// Add the character to our temporary line
$temp .= $char;
}
// Add our completed line to the output
$output .= $temp.$this->crlf;
}
// get rid of extra CRLF tacked onto the end
return substr($output, 0, strlen($this->crlf) * -1);
}