public static function prettyPrint($json, $options = array())
    {
        $tokens = preg_split('|([\{\}\]\[,])|', $json, -1, PREG_SPLIT_DELIM_CAPTURE);
        $result = "";
        $indent = 0;

        $ind = "    ";
        if (isset($options['indent'])) {
            $ind = $options['indent'];
        }

        $inLiteral = false;
        foreach ($tokens as $token) {
            $token = trim($token);
            if ($token == "") {
                continue;
            }

            if (preg_match('/^("(?:.*)"):[ ]?(.*)$/', $token, $matches)) {
                $token = $matches[1] . ': ' . $matches[2];
            }

            $prefix = str_repeat($ind, $indent);
            if (!$inLiteral && ($token == "{" || $token == "[")) {
                $indent++;
                if ($result != "" && $result[strlen($result)-1] == "\n") {
                    $result .= $prefix;
                }
                $result .= "$token\n";
            } elseif (!$inLiteral && ($token == "}" || $token == "]")) {
                $indent--;
                $prefix = str_repeat($ind, $indent);
                $result .= "\n$prefix$token";
            } elseif (!$inLiteral && $token == ",") {
                $result .= "$token\n";
            } else {
                $result .= ($inLiteral ?  '' : $prefix) . $token;

                //remove escaped backslash sequences causing false positives in next check
                $token = str_replace('\\', '', $token);
                // Count # of unescaped double-quotes in token, subtract # of
                // escaped double-quotes and if the result is odd then we are
                // inside a string literal
                if ((substr_count($token, '"')-substr_count($token, '\\"')) % 2 != 0) {
                    $inLiteral = !$inLiteral;
                }
            }
        }
        return $result;
    }
public static function prettyPrint($json, $options = array())
    {
        $tokens = preg_split('|([\{\}\]\[,])|', $json, -1, PREG_SPLIT_DELIM_CAPTURE);
        $result = "";
        $indent = 0;

        $ind = "    ";
        if (isset($options['indent'])) {
            $ind = $options['indent'];
        }

        $inLiteral = false;
        foreach ($tokens as $token) {
            $token = trim($token);
            if ($token == "") {
                continue;
            }

            if (preg_match('/^("(?:.*)"):[ ]?(.*)$/', $token, $matches)) {
                $token = $matches[1] . ': ' . $matches[2];
            }

            $prefix = str_repeat($ind, $indent);
            if (!$inLiteral && ($token == "{" || $token == "[")) {
                $indent++;
                if ($result != "" && $result[strlen($result)-1] == "\n") {
                    $result .= $prefix;
                }
                $result .= "$token\n";
            } elseif (!$inLiteral && ($token == "}" || $token == "]")) {
                $indent--;
                $prefix = str_repeat($ind, $indent);
                $result .= "\n$prefix$token";
            } elseif (!$inLiteral && $token == ",") {
                $result .= "$token\n";
            } else {
                $result .= ($inLiteral ?  '' : $prefix) . $token;

                //remove escaped backslash sequences causing false positives in next check
                $token = str_replace('\\', '', $token);
                // Count # of unescaped double-quotes in token, subtract # of
                // escaped double-quotes and if the result is odd then we are
                // inside a string literal
                if ((substr_count($token, '"')-substr_count($token, '\\"')) % 2 != 0) {
                    $inLiteral = !$inLiteral;
                }
            }
        }
        return $result;
    }
public static function prettyPrint ( $ json , $ options = array ( ) ) { $ tokens = preg_split ( '|([\{\}\]\[,])|' , $ json , - 1 , PREG_SPLIT_DELIM_CAPTURE ) ; $ result = "" ; $ indent = 0 ; $ ind = "    " ; if ( isset ( $ options [ 'indent' ] ) ) { $ ind = $ options [ 'indent' ] ; } $ inLiteral = false ; foreach ( $ tokens as $ token ) { $ token = trim ( $ token ) ; if ( $ token == "" ) { continue ; } if ( preg_match ( '/^("(?:.*)"):[ ]?(.*)$/' , $ token , $ matches ) ) { $ token = $ matches [ 1 ] . ': ' . $ matches [ 2 ] ; } $ prefix = str_repeat ( $ ind , $ indent ) ; if ( ! $ inLiteral && ( $ token == "{" || $ token == "[" ) ) { $ indent ++ ; if ( $ result != "" && $ result [ strlen ( $ result ) - 1 ] == "\n" ) { $ result .= $ prefix ; } $ result .= "$token\n" ; } elseif ( ! $ inLiteral && ( $ token == "}" || $ token == "]" ) ) { $ indent -- ; $ prefix = str_repeat ( $ ind , $ indent ) ; $ result .= "\n$prefix$token" ; } elseif ( ! $ inLiteral && $ token == "," ) { $ result .= "$token\n" ; } else { $ result .= ( $ inLiteral ? '' : $ prefix ) . $ token ; //remove escaped backslash sequences causing false positives in next check $ token = str_replace ( '\\' , '' , $ token ) ; // Count # of unescaped double-quotes in token, subtract # of // escaped double-quotes and if the result is odd then we are // inside a string literal if ( ( substr_count ( $ token , '"' ) - substr_count ( $ token , '\\"' ) ) % 2 != 0 ) { $ inLiteral = ! $ inLiteral ; } } } return $ result ; }
Pretty-print JSON string

Use 'indent' option to select indentation string - by default it's a tab

@param string $json Original JSON string
@param array $options Encoding options
@return string
Pretty - print JSON string