標籤:set turn gb2312 euc for encoding str 編碼格式 格式
- /**
- * 將非GBK字元集的編碼轉為GBK
- *
- * @param mixed $mixed 來源資料
- *
- * @return mixed GBK格式資料
- */
- function charsetToGBK($mixed)
- {
- if (is_array($mixed)) {
- foreach ($mixed as $k => $v) {
- if (is_array($v)) {
- $mixed[$k] = charsetToGBK($v);
- } else {
- $encode = mb_detect_encoding($v, array(‘ASCII‘, ‘UTF-8‘, ‘GB2312‘, ‘GBK‘, ‘BIG5‘));
- if ($encode == ‘UTF-8‘) {
- $mixed[$k] = iconv(‘UTF-8‘, ‘GBK‘, $v);
- }
- }
- }
- } else {
- $encode = mb_detect_encoding($mixed, array(‘ASCII‘, ‘UTF-8‘, ‘GB2312‘, ‘GBK‘, ‘BIG5‘));
- //var_dump($encode);
- if ($encode == ‘UTF-8‘) {
- $mixed = iconv(‘UTF-8‘, ‘GBK‘, $mixed);
- }
- }
- return $mixed;
- }
-
-
- /**
- * 將非UTF-8字元集的編碼轉為UTF-8
- *
- * @param mixed $mixed 來源資料
- *
- * @return mixed utf-8格式資料
- */
- function charsetToUTF8($mixed)
- {
- if (is_array($mixed)) {
- foreach ($mixed as $k => $v) {
- if (is_array($v)) {
- $mixed[$k] = charsetToUTF8($v);
- } else {
- $encode = mb_detect_encoding($v, array(‘ASCII‘, ‘UTF-8‘, ‘GB2312‘, ‘GBK‘, ‘BIG5‘));
- if ($encode == ‘EUC-CN‘) {
- $mixed[$k] = iconv(‘GBK‘, ‘UTF-8‘, $v);
- }
- }
- }
- } else {
- $encode = mb_detect_encoding($mixed, array(‘ASCII‘, ‘UTF-8‘, ‘GB2312‘, ‘GBK‘, ‘BIG5‘));
- if ($encode == ‘EUC-CN‘) {
- $mixed = iconv(‘GBK‘, ‘UTF-8‘, $mixed);
- }
- }
- return $mixed;
- }
編碼格式,網上看了資料還是亂碼,就試下這個吧