Ignore:
Timestamp:
Nov 15, 2014 12:00:02 AM (11 years ago)
Author:
dungnv
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • pro-violet-viettel/sourcecode/system/helpers/text_helper.php

    r289 r521  
    532532}
    533533
     534if ( ! function_exists('jsencode'))
     535{
     536        function jsencode( $obj, $json = false ){
     537                switch( gettype( $obj ) ){
     538                        case 'array':
     539                        case 'object':
     540                                $code = array();
     541                                // is it anything other than a simple linear array
     542                                if( array_keys($obj) !== range(0, count($obj) - 1) ){
     543                                        foreach( $obj as $key => $val ){
     544                                                $code []= $json ?
     545                                                '"' . $key . '":' . jsencode( $val ) :
     546                                                $key . ':' . jsencode( $val );
     547                                        }
     548                                        $code = '{' . implode( ',', $code ) . '}';
     549                                } else {
     550                                        foreach( $obj as $val ){
     551                                                $code []= jsencode( $val );
     552                                        }
     553                                        $code = '[' . implode( ',', $code ) . ']';
     554                                }
     555                                return $code;
     556                                break;
     557                        case 'boolean':
     558                                return $obj ? 'true' : 'false' ;
     559                                break;
     560                        case 'integer':
     561                        case 'double':
     562                                return floatVal( $obj );
     563                                break;
     564                        case 'NULL':
     565                        case 'resource':
     566                        case 'unknown':
     567                                return 'null';
     568                                break;
     569                        default:
     570                                return '"' . addslashes( $obj ) . '"';
     571                }
     572        }
     573}
     574
     575if(!function_exists('json_encode'))
     576{
     577        function json_encode($a=false)
     578        {
     579                // Some basic debugging to ensure we have something returned
     580                if (is_null($a)) return 'null';
     581                if ($a === false) return 'false';
     582                if ($a === true) return 'true';
     583                if (is_scalar($a))
     584                {
     585                        if (is_float($a))
     586                        {
     587                                // Always use '.' for floats.
     588                                return floatval(str_replace(',', '.', strval($a)));
     589                        }
     590                        if (is_string($a))
     591                        {
     592                                static $jsonReplaces = array(array('\\', '/', "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
     593                                return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
     594                        }
     595                        else
     596                                return $a;
     597                }
     598                $isList = true;
     599                for ($i = 0, reset($a); true; $i++) {
     600                        if (key($a) !== $i)
     601                        {
     602                                $isList = false;
     603                                break;
     604                        }
     605                }
     606                $result = array();
     607                if ($isList)
     608                {
     609                        foreach ($a as $v) $result[] = json_encode($v);
     610                        return '[' . join(',', $result) . ']';
     611                }
     612                else
     613                {
     614                        foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
     615                        return '{' . join(',', $result) . '}';
     616                }
     617        }
     618}
     619
     620if(!function_exists('json_decode'))
     621{
     622        function json_decode($json)
     623        {
     624                $comment = false;
     625                $out = '$x=';
     626                for ($i=0; $i<strlen($json); $i++)
     627                {
     628                if (!$comment)
     629                        {
     630                        if (($json[$i] == '{') || ($json[$i] == '['))
     631                                $out .= ' array(';
     632                                                else if (($json[$i] == '}') || ($json[$i] == ']'))
     633                                $out .= ')';
     634                                else if ($json[$i] == ':')
     635                                        $out .= '=>';
     636                                        else
     637                                        $out .= $json[$i];
     638                        }
     639                        else
     640                                $out .= $json[$i];
     641                                if ($json[$i] == '"' && $json[($i-1)]!="\\")
     642                                $comment = !$comment;
     643                        }
     644                        eval($out . ';');
     645                        return $x;
     646                }
     647                }
     648
    534649/* End of file text_helper.php */
    535650/* Location: ./system/helpers/text_helper.php */
Note: See TracChangeset for help on using the changeset viewer.