php5.4以下,json_encode不转义实现方法
2017-02-28 20:24
786 查看
/** * 不转义中文 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("\r", "\n", "\t", "\""), array('\r', '\n', '\t', '\\"'), $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 . ''; } }
相关文章推荐
- php5.4以下版本json不支持不转义内容中文的解决方法
- php5.4以下版本json不支持不转义内容中文的解决方法
- 解决PHP5.4版本以下json_encode转义中文的情况
- PHP JSON_ENCODE 不转义中文汉字的方法
- PHP JSON_ENCODE 不转义中文汉字的方法
- PHP JSON_ENCODE 不转义中文汉字的方法
- PHP JSON_ENCODE 不转义中文汉字的方法
- 黄聪:PHP JSON_ENCODE 不转义中文汉字的方法
- 从微信自定义菜单说php json_encode不转义中文汉字的方法
- PHP JSON_ENCODE 不转义中文汉字的方法
- PHP实现json_decode不转义中文的方法
- PHP中让json_encode不自动转义斜杠“/”的方法
- Datawindow通过Json接口实现数据操作时(三层)PHP5.2 json_encode时发现汉字转换成了\u表示的处理方法
- 微信自定义菜单说php json_encode不转义中文汉字的方法
- PHP JSON_ENCODE 不转义中文汉字的方法
- PHP JSON_ENCODE 不转义中文汉字的方法
- php json_encode 中文乱码的解决方法
- php json_encode 中文乱码的解决方法
- php中json_decode()和json_encode()的使用方法
- php中json_decode()和json_encode()的使用方法