escape utf8字符串的php实现

来源:百度文库 编辑:神马文学网 时间:2024/04/25 15:18:57
http://blog.iyi.cn/start/2007/04/escape_utf8php.html
网上很多javascript escape的php实现,都是针对gb2312的,主要缺点就是gb之外的字符不能正确escape。并且函数中字符的分界也有问题。所以对其进行了更新。
用了一晚上时间,仔细学习了一下字符编码的知识,将escape函数改为utf8版本,这样,就可以正确的编码所有的文字了。
目前写了两个版本:
function escape($str) {
preg_match_all("/[\xc2-\xdf][\x80-\xbf]+|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}|[\x01-\x7f]+/e",$str,$r);
//匹配utf-8字符,
$str = $r[0];
$l = count($str);
for($i=0; $i<$l; $i++){
$value = ord($str[$i][0]);
/*
if($value >= 192 && $value <= 223) $i++;//单字节
elseif($value >= 224 && $value <= 239) $i = $i + 2;//双字节
elseif($value >= 240 && $value <= 247) $i = $i + 3;//三字节
*/
if($value < 223){
$str[$i] = rawurlencode(utf8_decode($str[$i]));
//先将utf8编码转换为ISO-8859-1编码的单字节字符,urlencode单字节字符.
//utf8_decode()的作用相当于iconv("UTF-8","CP1252",$v)。
}else{
$str[$i] = "%u".strtoupper(bin2hex(iconv("UTF-8","UCS-2",$str[$i])));
}
}
return join("",$str);
}
这个版本使用循环,逐字escape。
 
function escape_callback($matches){ $value = ord($matches[1][0]); /* if($value >= 192 && $value <= 223) $i++;//单字节 elseif($value >= 224 && $value <= 239) $i = $i + 2;//双字节 elseif($value >= 240 && $value <= 247) $i = $i + 3;//三字节 */ if($value < 223){ $str = rawurlencode(utf8_decode($matches[1])); //先将utf8编码转换为ISO-8859-1编码的单字节字符,urlencode单字节字符. //utf8_decode()的作用相当于iconv("UTF-8","CP1252",$v)。 }else{ $str = "%u".strtoupper(bin2hex(iconv("UTF-8","UCS-2",$matches[1]))); } return $str; } function pescape($uri){ return preg_replace_callback(‘/([\xc2-\xdf][\x80-\xbf]+|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}|[\x01-\x7f]+)/‘, ‘escape_callback‘, $uri); }这个版本用preg_replace_callback自动迭代。
两个版本的效率都很快,都在毫秒以下级。