public function EncodeQ($str, $position = 'text')
{
/* There should not be any EOL in the string */
$encoded = preg_replace("[\r\n]", '', $str);
switch (strtolower($position)) {
case 'phrase':
$encoded = preg_replace("/([^A-Za-z0-9!*+\\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
break;
case 'comment':
$encoded = preg_replace("/([\\(\\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
case 'text':
default:
/* Replace every high ascii, control =, ? and _ characters */
$encoded = preg_replace('/([\\000-\\011\\013\\014\\016-\\037\\075\\077\\137\\177-\\377])/e', "'='.sprintf('%02X', ord('\\1'))", $encoded);
break;
}
/* Replace every spaces to _ (more readable than =20) */
$encoded = str_replace(' ', '_', $encoded);
return $encoded;
}