/*============================================================================*/
网络编程的 tcp 编码 中文汉字处理
/*============================================================================*/
function cleanup($string)
{
$string = str_replace(array("/r", "/n", "/r/n"), "", $string);
$string = trim($string);
return $string;
}
function utf8_encode_c($string)
{
$charset = 'utf-8';
$encoded_string = $string;
if(strtolower($charset) == 'utf-8' && preg_match('/[/x00-/x08/x0b/x0c/x0e-/x1f/x7f-/xff]/', $string))
{
// Define start delimimter, end delimiter and spacer
$end = "?=";
$start = "=?" . $charset . "?B?";
$spacer = $end . ' ' . $start;
// Determine length of encoded text within chunks and ensure length is even (should NOT use the my_strlen functions)
$length = 75 - strlen($start) - strlen($end);
$length = floor($length/4) * 4;
// Encode the string and split it into chunks with spacers after each chunk
$encoded_string = base64_encode($encoded_string);
$encoded_string = chunk_split($encoded_string, $length, $spacer);
// Remove trailing spacer and add start and end delimiters
$spacer = preg_quote($spacer);
$encoded_string = preg_replace("/" . $spacer . "$/", "", $encoded_string);
$encoded_string = $start . $encoded_string . $end;
}
return $encoded_string;
}