Fun things speak php json file with the escape symbol

php array of time-tested and json

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

  • Please inform yourself about these

Examples of groups attach operation

  1. Examples of a means of function 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. Examples of two, and by 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);
    

postscript

  • htmlentities and htmlspecialchars also the brain make it, I felt really good, hee hee! !

Guess you like

Origin www.cnblogs.com/jjxhp/p/11735776.html