编码中文汉字处理

 /*============================================================================*/
                 网络编程的    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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值