php中文亂碼轉(zhuǎn)換的解決辦法:1、設(shè)置編碼為“header('Content-Type:text/html;charset=utf-8');”;2、使用“mb_convert_encoding”等函數(shù)進行轉(zhuǎn)換。
推薦:《PHP視頻教程》
PHP代碼中文輸出亂碼和轉(zhuǎn)碼問題
1.header('Content-Type:text/html;charset=utf-8'); 防止json格式的中文亂碼輸出,在輸出之前寫出此代碼行
2.字符轉(zhuǎn)碼:$a為待轉(zhuǎn)碼字符串,$encode為 $a的編碼規(guī)則,$to_encode 為$a 將要轉(zhuǎn)的編碼規(guī)則,$str_encode 轉(zhuǎn)碼后的字符串,
(一): $encode = mb_detect_encoding($a, array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5'));//獲取當前字符串的編碼 $str_encode = mb_convert_encoding($a, $to_encode, $encode);//將字符編碼改為$to_encode (二):$str_encode = iconv($encode, $to_encode, $a);//例:$A = iconv("gbk", "utf-8", $A); (三):/** * 1.自動識別編碼并轉(zhuǎn)換為UTF-8 */ function characet($data){ if( !empty($data) ){ $fileType = mb_detect_encoding($data , array('UTF-8','GBK','LATIN1','BIG5')) ; if( $fileType != 'UTF-8'){ $data = mb_convert_encoding($data ,'utf-8' , $fileType); } } return $data; }
3.自己寫了一個小得處理PHP代碼http調(diào)試時輸出漢字的代碼:
//防止中文轉(zhuǎn)碼,遍歷數(shù)據(jù)結(jié)果,每項單獨urlencode, public function arrayUrlencode($array) { if (empty($array)) { return $array; } else { foreach ($array as $key =>$value) {//對每個數(shù)組元素進行urlencode if (is_array($value)) { $array[$key] =$this->arrayUrlencode($value); } else { $array[$key] =urlencode($value); } } } return $array; } //再整體urldecode public function arrayJsonencode($array) { $url_arr =$this->arrayUrlencode($array); $json_arr = json_encode($url_arr);//json 輸出 return urldecode($json_arr); //整體urldecode }