php转换数据格式

  • 将数据格式转换iconv
$gbk = '%CA%F3%2C%BB%A2%2F%CA%F3%2C%C1%FA%2F%BB%A2%2C%C1%FA';
$res = iconv("GBK","UTF-8",$gbk);

$decode = urldecode($res);
echo $decode;

其实这个例子直接urldecode()也能解决
php转换数据格式_第1张图片
图片.png

多维数组转换编码

 $value){
            $key = iconv($fromCharset, $toCharset, $key);
            $arr1[$key] = arrIconv($value, $fromCharset, $toCharset);
        }
    }else{
        $arr1 = iconv($fromCharset, $toCharset, $arr);
    }
    return $arr1;
}

$arr = array("中文" => "德问", "二维" => array("二维内容"));
$arrGBK = arrIconv($arr, "UTF-8", "GBK");
$arrUTF = arrIconv($arrGBK, "GBK", "UTF-8");

var_dump($arrGBK);
echo '
';
var_dump($arrUTF);
echo '
';

UTF8与GBK格式的转换

// GBK 转 UTF-8
function utf($arr){
    if(is_array($arr)){
        $arr1 = array();
        foreach($arr as $key => $value){
            $key = iconv("GBK", "UTF-8//IGNORE",  $key);
            $arr1[$key] = utf($value, "GBK", "UTF-8//IGNORE");
        }
    }else{
        $arr1 = iconv("GBK", "UTF-8//IGNORE",$arr);
    }
    return $arr1;
}
// UTF-8 转 GBK
function gbk($str){
    if (is_array($str)) {
        $_tmp = array();
        foreach ($str as $key => $value) {
            $k = iconv("UTF-8", "gb2312//IGNORE", $key);
            $v = iconv("UTF-8", "gb2312//IGNORE", $value);
            $_tmp[$k] = $v;
        }
        return $_tmp;
    }
    return iconv("UTF-8", "gb2312//IGNORE", $str);
}

你可能感兴趣的:(php转换数据格式)