Prevent quoting of certain values with PHP json_encode()

后端 未结 7 1510
广开言路
广开言路 2021-01-06 01:21

When using PHP\'s json_encode to encode an array as a JSON string, is there any way at all to prevent the function from quoting specific values in the returned string? The r

7条回答
  •  滥情空心
    2021-01-06 01:34

    Following the lead of Stefan Gehrig I put this rough little class together. Example below. One must remember to use the serialize method if one has used the mark method, otherwise the markers will persist in the final json.

    class json_extended {
    
        public static function mark_for_preservation($str) {
            return 'OINK' . $str . 'OINK'; // now the oinks will be next to the double quotes
        }
    
        public static function serialize($stuff) {
            $json = json_encode($stuff);
            $json = str_replace(array('"OINK', 'OINK"'), '', $json);
            return $json;
        }
    }
    
    
    $js_arguments['submitHandler'] = json_extended::mark_for_preservation('handle_submit');
    
    
    

提交回复
热议问题