【php开源代码栏目提醒】:本文主要为网学会员提供compat.php,希望对需要compat.php网友有所帮助,学习一下!
<?php
// +----------------------------------------------------------------------
// | ThinkPHP
// +----------------------------------------------------------------------
// | Copyright (c) 2007 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$
/**
+------------------------------------------------------------------------------
* Think鍏煎鍑芥暟搴?閽堝5.2.0浠ヤ笅鐗堟湰
+------------------------------------------------------------------------------
* @category Think
* @package Common
* @author liu21st <liu21st@gmail.com>
* @version $Id$
+------------------------------------------------------------------------------
*/
if (!function_exists('json_encode')) {
function format_json_value(&$value)
{
if(is_bool($value)) {
$value = $value?'true':'false';
}elseif(is_int($value)) {
$value = intval($value);
}elseif(is_float($value)) {
$value = floatval($value);
}elseif(defined($value) && $value === null) {
$value = strval(constant($value));
}elseif(is_string($value)) {
$value = '"'.addslashes($value).'"';
}
return $value;
}
function json_encode($data)
{
if(is_object($data)) {
//瀵硅薄杞崲鎴愭暟缁?
$data = get_object_vars($data);
}else if(!is_array($data)) {
// 鏅氭牸寮忕洿鎺ヨ緭鍑?
return format_json_value($data);
}
// 鍒ゆ柇鏄惁鍏宠仈鏁扮粍
if(empty($data) || is_numeric(implode('',array_keys($data)))) {
$assoc = false;
}else {
$assoc = true;
}
// 缁勮 Json瀛楃涓?
$json = $assoc ? '{' : '[' ;
foreach($data as $key=>$val) {
if(!is_null($val)) {
if($assoc) {
$json .= "\"$key\":".json_encode($val).",";
}else {
$json .= json_encode($val).",";
}
}
}
if(strlen($json)>1) {// 鍔犱笂鍒ゆ柇 闃叉绌烘暟缁?
$json = substr($json,0,-1);
}
$json .= $assoc ? '}' : ']' ;
return $json;
}
}
if (!function_exists('json_decode')) {
function json_decode($json,$assoc=false)
{
// 鐩墠涓嶆敮鎸佷簩缁存暟缁勬垨瀵硅薄
$begin = substr($json,0,1) ;
if(!in_array($begin,array('{','['))) {
// 涓嶆槸瀵硅薄鎴栬呮暟缁勭洿鎺ヨ繑鍥?
return $json;
}
$parse = substr($json,1,-1);
$data = explode(',',$parse);
if($flag = $begin =='{' ) {
// 杞崲鎴怭HP瀵硅薄
$result = new stdClass();
foreach($data as $val) {
$item = explode(':',$val);
$key = substr($item[0],1,-1);
$result->$key = json_decode($item[1],$assoc);
}
if($assoc) {
$result = get_object_vars($result);
}
}else {
// 杞崲鎴怭HP鏁扮粍
$result = array();
foreach($data as $val) {
$result[] = json_decode($val,$assoc);
}
}
return $result;
}
}
if (!function_exists('property_exists')) {
/**
+----------------------------------------------------------
* 鍒ゆ柇瀵硅薄鐨勫睘鎬ф槸鍚﹀瓨鍦?
PHP5.1.0浠ヤ笂宸茬粡瀹氫箟
+----------------------------------------------------------
* @param object $class 瀵硅薄瀹炰緥
* @param string $property 灞炴у悕绉?
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
function property_exists($class, $property) {
if (is_object($class))
$class = get_class($class);
return array_key_exists($property, get_class_vars($class));
}
}
/**
+----------------------------------------------------------
* stripslashes鎵╁睍 鍙敤浜庢暟缁?
+----------------------------------------------------------
* @param mixed $value 鍙橀噺
+----------------------------------------------------------
* @return mixed
+----------------------------------------------------------
*/
if(!function_exists('stripslashes_deep')) {
function stripslashes_deep($value) {
$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
return $value;
}
}
?>