GBK的頁面輸出JSON格式的php函數
更新時間:2010年02月16日 13:38:35 作者:
encode之前轉換為utf-8,decode之后轉回gbk:
復制代碼 代碼如下:
function tb_json_encode($value, $options = 0)
{
return json_encode(tb_json_convert_encoding($value, “GBK”, “UTF-8″));
}
function tb_json_decode($str, $assoc = false, $depth = 512)
{
return tb_json_convert_encoding(json_decode($str, $assoc), “UTF-8″, “GBK”);
}
function tb_json_convert_encoding($m, $from, $to)
{
switch(gettype($m)) {
case ‘integer':
case ‘boolean':
case ‘float':
case ‘double':
case ‘NULL':
return $m;
case 'string':
return mb_convert_encoding($m, $to, $from);
case ‘object':
$vars = array_keys(get_object_vars($m));
foreach($vars as $key) {
$m->$key = tb_json_convert_encoding($m->$key, $from ,$to);
}
return $m;
case ‘array':
foreach($m as $k => $v) {
$m[tb_json_convert_encoding($k, $from, $to)] = tb_json_convert_encoding($v, $from, $to);
}
return $m;
default:
}
return $m;
}
相關文章
解析PHP的Yii框架中cookie和session功能的相關操作
這篇文章主要介紹了PHP的Yii框架中cookie和session功能的相關操作,需要的朋友可以參考下2016-03-03詳解php 使用Callable Closure強制指定回調類型
這篇文章主要介紹了詳解php 使用Callable Closure強制指定回調類型的相關資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10