编码中文汉字处理
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;
}
网络编程的 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;
}
相关文章推荐
- Linux ubuntu下python处理中文(汉字及标点)的编码设置
- Android学习札记9:对URL链接中中文汉字的编码处理
- Python处理unicode编码的txt文件(Python中文处理)——解决to_excel()和to_csv()导出文件内容为空的问题
- URL中的汉字和特殊字符编码,以及ASIHTTPRequest对此的处理
- ******英文字母和中文汉字在不同字符集编码下的字节数 英文字母:******
- 中文在C/C++中的处理和汉字乱码问题
- 将unicode编码的中文汉字转变成UTF-8编码的php程序
- python2.7.x的字符串编码到底什么鬼?(中文和英文的处理)
- 中文在C/C++中的处理和汉字乱码问题(wchar_t)
- [学习笔记]jsp中超级链接 中文编码处理
- 在js中处理特殊字符对应的中文编码
- UTF-8编码下'\u7528\u6237'转换为中文汉字'用户'
- python 处理中文遇到的编码问题总结 以及 字符str的编码如何判断
- python 处理中文文件时的编码问题,尤其是utf-8和gbk
- javascript 处理返回json中的\u中文乱码问题(也不是乱码了,就是\u编码)
- 【JavaEE】javaee中汉字中文值乱码问题处理
- 英文字母和中文汉字在不同字符集编码下的字节数
- 通过读写文本文件小结“关于python处理中文编码的问题”
- 关于js中用base64编码处理中文的问题
- python处理中文编码