您的位置:首页 > 其它

编码中文汉字处理

2009-11-13 18:23 134 查看
/*============================================================================*/
网络编程的 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: