ajax韩文处理

function iconv_ex($in_charset, $out_charset, $ar )
{
    if( is_string($ar) ) {
        $ar = iconv($in_charset, $out_charset, $ar);
    }
    if( !is_array($ar) ) {
        return $ar;
    }
    foreach($ar as &$v) {
        if( is_array($v) ) {
            $v = iconv_ex($in_charset, $out_charset, $v);
        }
        elseif( is_string($v) ) {
            $v = iconv($in_charset, $out_charset, $v);
        }
    }
    return $ar;
}

function nochaoscode($str,$encode='UTF-8') { 
	$str = iconv($encode, "UTF-16BE", $str); 
	for ($i = 0; $i < strlen($str); $i++,$i++) { 
		$code = ord($str{$i}) * 256 + ord($str{$i + 1}); 
		if ($code < 128) { 
			$output .= chr($code); 
		} else if ($code != 65279) { 
			$output .= "&#".$code.";"; 
		} 
	}
	return $output; 
} 

$commen_gb2312 = iconv_ex('UTF-8', 'GB2312',$content);
$commen_utf8 = $content;
$len_gb2312 = mb_strlen($commen_gb2312,'GB2312');
$len_utf8 = mb_strlen($commen_utf8,'UTF-8');
if ($len_utf8 == $len_gb2312) {
	$content	= $commen_gb2312;
} else {
	$content	= nochaoscode($commen_utf8);
}

你可能感兴趣的:(Ajax)