JS中文字符串转换unicode编码函数(0 位领导批示)

AJAX传递中文字符串时必须把中文字符串编码成unicode,一般会用到JS的自带函数escape().不过找到了更好的函数来确决中文字符转换成unicode编码的函数

  1. function uniencode(text)
  2. {
  3.     text = escape(text.toString()).replace(/\+/g, "%2B");
  4.     var matches = text.match(/(%([0-9A-F]{2}))/gi);
  5.     if (matches)
  6.     {
  7.         for (var matchid = 0; matchid < matches.length; matchid++)
  8.         {
  9.             var code = matches[matchid].substring(1,3);
  10.             if (parseInt(code, 16) >= 128)
  11.             {
  12.                 text = text.replace(matches[matchid], '%u00' + code);
  13.             }
  14.         }
  15.     }
  16.     text = text.replace('%25', '%u0025');
  17.  
  18.     return text;
  19. }

当然服务器端要对编码过的字符串进行第二次转码.把字符串转换成UTF-8编码.

全文阅读 »

UTF-8编码截取中文字符串函数(1 位领导批示)

记录一下,非常好用

/**
* UTF-8 中文切字
*
@param    string    需要切分的字符串
*
@param    int    切分开始处
*
@param    int    切分的长度
*
@return    int    切分后的字符串
*/

 
function msubstr($str, $start, $length=NULL)
{
    
preg_match_all("/./u", $str, $ar);
 
    
if(func_num_args() >= 3) {
      
$end = func_get_arg(2);
      
return join("",array_slice($ar[0],$start,$end));
    
} else {
      
return join("",array_slice($ar[0],$start));
    
}
}

随机显示的10篇日志

评论最多的10篇日志

浏览最多的10篇日志