使用php函数编码邮件头里的主题栏(subject)

王朝php·作者佚名  2005-09-09
窄屏简体版  字體: |||超大  

[code] function encode_header ($str, $position = 'text') {

$x = 0;

switch (strtolower($position)) {

case 'phrase':

if (preg_match_all('/[\200-\377]/', $str, $matches) == 0) {

// Can't use addslashes as we don't know what value has magic_quotes_sybase.

$encoded = addcslashes($str, '\000-\037\177');

$encoded = preg_replace('/([\"])/', '\\"', $encoded);

if (($str == $encoded) && (preg_match_all('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str, $matches) == 0))

return ($encoded);

else

return ("\"$encoded\"");

}

$x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);

break;

case 'comment':

$x = preg_match_all('/[()"]/', $str, $matches);

// Fall-through

case 'text':

default:

$x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);

break;

}

if ($x == 0)

return ($str);

$maxlen = 75 - 7 - strlen($this->CharSet);

// Try to select the encoding which should produce the shortest output

if (strlen($str)/3 < $x) {

$encoding = 'B';

$encoded = base64_encode($str);

$maxlen -= $maxlen % 4;

$encoded = trim(chunk_split($encoded, $maxlen, "\n"));

} else {

$encoding = 'Q';

$encoded = $this->encode_q($str, $position);

$encoded = $this->word_wrap($encoded, $maxlen, true);

$encoded = str_replace("=".$this->LE, "\n", trim($encoded));

}

$encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);

$encoded = trim(str_replace("\n", $this->LE, $encoded));

return($encoded);

}

[/code]

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航