復(fù)制代碼 代碼如下:
// 自動轉(zhuǎn)換字符集 支持數(shù)組轉(zhuǎn)換
function auto_charset($fContents, $from='gbk', $to='utf-8') {
$from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
$to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
if (strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents))) {
//如果編碼相同或者非字符串標量則不轉(zhuǎn)換
return $fContents;
}
if (is_string($fContents)) {
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding($fContents, $to, $from);
} elseif (function_exists('iconv')) {
return iconv($from, $to, $fContents);
} else {
return $fContents;
}
} elseif (is_array($fContents)) {
foreach ($fContents as $key => $val) {
$_key = auto_charset($key, $from, $to);
$fContents[$_key] = auto_charset($val, $from, $to);
if ($key != $_key)
unset($fContents[$key]);
}
return $fContents;
}
else {
return $fContents;
}
}
復(fù)制代碼 代碼如下:
function isGb2312($string) {
for($i=0; $i 127) {
if( ($v >= 228) && ($v < = 233) )
{
if( ($i+2) >= (strlen($string) - 1)) return true;
$v1 = ord( $string[$i+1] );
$v2 = ord( $string[$i+2] );
if( ($v1 >= 128) && ($v1 < =191) && ($v2 >=128) && ($v2 < = 191) )
return false;
else
return true;
}
}
}
return true;
}
function isUtf8($string) {
return preg_match('%^(?:
[/x09/x0A/x0D/x20-/x7E] # ASCII
| [/xC2-/xDF][/x80-/xBF] # non-overlong 2-byte
| /xE0[/xA0-/xBF][/x80-/xBF] # excluding overlongs
| [/xE1-/xEC/xEE/xEF][/x80-/xBF]{2} # straight 3-byte
| /xED[/x80-/x9F][/x80-/xBF] # excluding surrogates
| /xF0[/x90-/xBF][/x80-/xBF]{2} # planes 1-3
| [/xF1-/xF3][/x80-/xBF]{3} # planes 4-15
| /xF4[/x80-/x8F][/x80-/xBF]{2} # plane 16
)*$%xs', $string);
}
新聞熱點
疑難解答