本文實(shí)例講述了PHP實(shí)現(xiàn)自動(dòng)識(shí)別原編碼并對(duì)字符串進(jìn)行編碼轉(zhuǎn)換的方法。分享給大家供大家參考,具體如下:
/**
* 對(duì)數(shù)據(jù)進(jìn)行編碼轉(zhuǎn)換
* @param array/string $data 數(shù)組
* @param string $output 轉(zhuǎn)換后的編碼
* Created on 2016-7-13
*/
function array_iconv($data, $output = 'utf-8') {
$encode_arr = array('UTF-8','ASCII','GBK','GB2312','BIG5','JIS','eucjp-win','sjis-win','EUC-JP');
$encoded = mb_detect_encoding($data, $encode_arr);
if (!is_array($data)) {
return mb_convert_encoding($data, $output, $encoded);
}
else {
foreach ($data as $key=>$val) {
$key = array_iconv($key, $output);
if(is_array($val)) {
$data[$key] = array_iconv($val, $output);
} else {
$data[$key] = mb_convert_encoding($data, $output, $encoded);
}
}
return $data;
}
}
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。