zoukankan      html  css  js  c++  java
  • php5.4以下,json_encode不转义实现方法

        /**
         * 不转义中文 json_encode
         * 中文转义成 unicode 字符的话不方便后台日志搜索,不转义吧
         * Add By TuJia
         */
        protected function json_encode($input){
            // 从 PHP 5.4.0 起, 增加了这个选项.
            if(defined('JSON_UNESCAPED_UNICODE')){
                return json_encode($input, JSON_UNESCAPED_UNICODE);
            }
            if(is_string($input)){
                $text = $input;
                $text = str_replace('\', '\\', $text);
                $text = str_replace(
                    array("
    ", "
    ", "	", """),
                    array('
    ', '
    ', '	', '\"'),
                    $text);
                return '"' . $text . '"';
            }else if(is_array($input) || is_object($input)){
                $arr = array();
                $is_obj = is_object($input) || (array_keys($input) !== range(0, count($input) - 1));
                foreach($input as $k=>$v){
                    if($is_obj){
                        $arr[] = self::json_encode($k) . ':' . self::json_encode($v);
                    }else{
                        $arr[] = self::json_encode($v);
                    }
                }
                if($is_obj){
                    return '{' . join(',', $arr) . '}';
                }else{
                    return '[' . join(',', $arr) . ']';
                }
            }else if(is_null($input)){
                return 'null';
            }else if(is_bool($input)){
                return $input? 1 : 0;
            }else{
                return $input . '';
            }
        }
  • 相关阅读:
    go timer
    go语言函数作为参数传递
    gomail发送附件
    统计和图片来说话
    一个发邮件的demo 用golang
    Shelled-out Commands In Golang
    阅读笔记
    语言和人收藏
    在线学习网址
    通用首席娱乐官:了解自己是成功的基础
  • 原文地址:https://www.cnblogs.com/tujia/p/6480741.html
Copyright © 2011-2022 走看看