thinkphp3.2 框架函数库中找到两个函数使用了json_decode()函数。

/**

  加载配置文件 支持格式转换 仅支持一级配置
 
@param string $file 配置文件名
  @param string $parse 配置解析方法 有些格式需要用户自己解析
 
@return array
 */
function load_config($file,$parse=CONF_PARSE){
    $ext  = pathinfo($file,PATHINFO_EXTENSION);
    switch($ext){
        case 'php':
            return include $file;
        case 'ini':
            return parse_ini_file($file);
        case 'yaml':
            return yaml_parse_file($file);
        case 'xml': 
            return (array)simplexml_load_file($file);
        case 'json':
            return json_decode(file_get_contents($file), true);
        default:
            if(function_exists($parse)){
                return $parse($file);
            }else{
                E(L('_NOTSUPPORT').':'.$ext);
            }
    }

}

以上加载配置文件函数, 支持格式转换 仅支持一级配置,也用到json_decode()函数。

json_decode(file_get_contents($file), true)

file_get_contents() 函数把整个文件读入一个字符串中,第二个参数为true,把字符串(类似json的配置文件)强制转换为数组(键值对数组),return 返回键值对数组

/**
 * Cookie 设置、获取、删除
 * @param string $name cookie名称
 * @param mixed $value cookie值
 * @param mixed $option cookie参数
 * @return mixed
 */
function cookie($name='', $value='', $option=null) {
    // 默认设置
    $config = array(
        'prefix'    =>  C('COOKIE_PREFIX'), // cookie 名称前缀
        'expire'    =>  C('COOKIE_EXPIRE'), // cookie 保存时间
        'path'      =>  C('COOKIE_PATH'), // cookie 保存路径
        'domain'    =>  C('COOKIE_DOMAIN'), // cookie 有效域名
        'httponly'  =>  C('COOKIE_HTTPONLY'), // httponly设置
    );
    // 参数设置(会覆盖黙认设置)
    if (!is_null($option)) {
        if (is_numeric($option))
            $option = array('expire' => $option);
        elseif (is_string($option))
            parse_str($option, $option);
        $config     = array_merge($config, array_change_key_case($option));
    }
    if(!empty($config['httponly'])){
        ini_set("session.cookie_httponly", 1);
    }
    // 清除指定前缀的所有cookie
    if (is_null($name)) {
        if (empty($_COOKIE))
            return null;
        // 要删除的cookie前缀,不指定则删除config设置的指定前缀
        $prefix = empty($value) ? $config['prefix'] : $value;
        if (!empty($prefix)) {// 如果前缀为空字符串将不作处理直接返回
            foreach ($_COOKIE as $key => $val) {
                if (0 === stripos($key, $prefix)) {
                    setcookie($key, '', time() - 3600, $config['path'], $config['domain']);
                    unset($_COOKIE[$key]);
                }
            }
        }
        return null;
    }elseif('' === $name){
        // 获取全部的cookie
        return $_COOKIE;
    }
    $name = $config['prefix'] . str_replace('.', '_', $name);
    if ('' === $value) {
        if(isset($_COOKIE[$name])){
            $value =    $_COOKIE[$name];
            if(0===strpos($value,'think:')){
                $value  =   substr($value,6);
                return array_map('urldecode',json_decode(MAGIC_QUOTES_GPC?stripslashes($value):$value,true));
            }else{
                return $value;
            }
        }else{
            return null;
        }
    } else {
        if (is_null($value)) {
            setcookie($name, '', time() - 3600, $config['path'], $config['domain']);
            unset($_COOKIE[$name]); // 删除指定cookie
        } else {
            // 设置cookie
            if(is_array($value)){
                $value  = 'think:'.json_encode(array_map('urlencode',$value));
            }
            $expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0;
            setcookie($name, $value, $expire, $config['path'], $config['domain']);
            $_COOKIE[$name] = $value;
        }
    }
    return null;
}

在Cookie的设置、获取、删除函数中有:

json_decode(MAGIC_QUOTES_GPC?stripslashes($value):$value,true)。

简单介绍下magic_quotes_gpc(魔术引号开关)

php中的magic_quotes_gpc是配置在php.ini中的,他的作用类似addslashes(),就是对输入的字符创中的字符进行转义处理。他可以对$_POST、$_GET以及进行数据库操作的sql进行转义处理,防止sql注入。

简单介绍下stripslashes():

删除反斜杠 echo stripslashes("Who's Bill Gates?"); 输出:Who's Bill Gates?

所以当magic_quotes_gpc开启,stripslashes函数删除字符中的反斜杠,反之则不删除。

json_decode()函数简单介绍下:

json_decode(),对json格式的字符串进行解码,并转换为 PHP 变量。

对键值对的json数据进行编码成对象数组,如果第二个参数为true则强制转换为数组。

那么json_encode()怎么用?

可以将对象转为json格式和将数组(键值对)转换为json格式。

那么函数中:json_decode(MAGIC_QUOTES_GPC?stripslashes($value):$value,true)输出什么呢?

MAGIC_QUOTES_GPC为true时:删除字符串中的反斜杠(无论字符串中有无反斜杠),都输出没有反斜杠的字符串。

MAGIC_QUOTES_GPC为false时:当字符串中没有反斜杠则返回$value的值(原字符串),如果有反斜杠则返回NULL。

json_encode()和json_decode()是编译和反编译过程,注意json只接受utf-8编码的字符,所以json_encode()的参数必须是utf-8编码,否则会得到空字符或者null。

点赞(1) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部