首页 > Web开发 > 详细

php讲转义符号与json文件的趣事情

时间:2019-10-25 00:20:08      阅读:100      评论:0      收藏:0      [点我收藏+]

php中屡试不爽的数组和json

  • json_encode与json_decode
  • urlencode与urldecode
  • addslashes与stripslashes
  • addcslashes与stripcslashes

  • 以上这些请自行了解

附上两组操作实例

  1. 实例一,借助urlencode函数

    function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
    {
        static $recursive_counter = 0;
        if (++$recursive_counter > 1000) {
            die('possible deep recursion attack');
        }
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                arrayRecursive($array[$key], $function, $apply_to_keys_also);
            } else {
                $array[$key] = $function($value);
            }
    
            if ($apply_to_keys_also && is_string($key)) {
                $new_key = $function($key);
                if ($new_key != $key) {
                    $array[$new_key] = $array[$key];
                    unset($array[$key]);
                }
            }
        }
        $recursive_counter--;
    }
    
    function my_json($array) {
        arrayRecursive($array, 'urlencode', true);
        $json = json_encode($array);
        return urldecode($json);
    }
    $arr = [
        'a'  => 123,
        'b'  => 456,
        'http://www.jjxhp.com/abc/sasa.php' => 'tstst你好',
        'http://www.baidu.com/' => '我们都是好人吧',
        'rew你好' => 'dsdsqwq'
    ];
    
    $str = my_json($arr);
    
    file_put_contents(__DIR__.'/json_str.txt', $str);
    
  2. 实例二,借助 json_encode与stripslashes

    $new_str = json_encode($arr, JSON_UNESCAPED_UNICODE);
    file_put_contents(__DIR__.'/new_json_str.txt', $new_str);
    $new_str1 = stripslashes($new_str);
    file_put_contents(__DIR__.'/new_json_str1.txt', $new_str1);
    

附言

  • htmlentities与htmlspecialchars也脑补吧,自己感觉真的很好用,嘻嘻!!

php讲转义符号与json文件的趣事情

原文:https://www.cnblogs.com/jjxhp/p/11735776.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!