// 定义一个将unicode转义成中文的办法
function unicode2Chinese($str)
{
return preg_replace_callback("#u([0-9a-f]{4})#i",
function ($r) {return iconv('UCS-2BE', 'UTF-8', pack('H4', $r[1]));},
$str);
}
public function unicodeString($str, $encoding=null) {
return preg_replace_callback('/u([0-9a-fA-F]{4})/u',
create_function('$match', 'return
mb_convert_encoding(pack("H*", $match[1]), "utf-8", "UTF-16BE");'), $str);
}
//将数组转义成json字符串 然后再将unicode转义成中文
$info=$this->unicodeString(json_encode($cache));
file_put_contents("typeinfo.json", $info);
// 定义一个将unicdeo转义成中文的办法
function unicodeDecode($unicode_str){
$json = '{"str":"'.$unicode_str.'"}';
$arr = json_decode($json,true);
if(empty($arr)) return '';
return $arr['str'];
}
// 定义一个将中文转义成unicode的办法
function UnicodeEncode($str){
preg_match_all('/./u',$str,$matches);
$unicodeStr = "";
foreach($matches[0] as $m){
//拼接
$unicodeStr .= "&#".base_convert(bin2hex(iconv('UTF-8',"UCS-4",$m)),16,10);
}
return $unicodeStr;
}
// 定义一个将unicode转义成中文的办法
function unicode2Chinese($str)
{
return preg_replace_callback("#u([0-9a-f]{4})#i",
function ($r) {return iconv('UCS-2BE', 'UTF-8', pack('H4', $r[1]));},
$str);
}
//定义一个数组
$arr = array('code' => 200,
'url' => "http://www.baidu.com",
'type' =>'m3u8',
'msg' =>"解析失败");
//将数组转义成json字符串
$json_str=json_encode($arr);
//将json字符串(本质就是字符串 无非就是json格式的字符串罢了) 写入文件中
$info=unicode2Chinese($json_str);
file_put_contents("typeinfo.json", $info);
//获取文件中的json字符串
$jsonStr=file_get_contents("typeinfo.json");
//将获得的json字符串转义成json对象
$json_object=json_decode($jsonStr);
/* $json_object= stdClass Object
(
[code] => 200
[type] => m3u8
[url] => http://www.baidu.com
[msg] => 解析失败
)*/
//将获得的json字符串转义成数组对象
$json_arr=json_decode($jsonStr,true);
/* $json_arr=Array
(
[code] => 200
[type] => m3u8
[url] => http://www.baidu.com
[msg] => 解析失败
)*/
//判断它是不是数组对象
if(is_array($json_object)){
echo "是数组";
}else{
echo "不是数组";
}
扫描二维码,在手机上阅读
版权说明
文章采用: 《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权。版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!