src/Utility/Utility.php
public static function prettyJson($json, $indString = "\t") {
		// Replace any escaped \" marks so we don't get tripped up on quotemarks_counter
		$tokens = preg_split('|([\{\}\]\[,])|', str_replace('\"', '~~PRETTY_JSON_QUOTEMARK~~', $json), -1, PREG_SPLIT_DELIM_CAPTURE);

		$indent = 0;
		$result = '';
		$quotemarksCounter = 0;
		$nextTokenUsePrefix = true;

		foreach ($tokens as $token) {
			$quotemarksCounter = $quotemarksCounter + (count(explode('"', $token)) - 1);

			if ($token === '') {
				continue;
			}

			if ($nextTokenUsePrefix) {
				$prefix = str_repeat($indString, $indent);
			} else {
				$prefix = null;
			}

			// Determine if the quote marks are open or closed
			if ($quotemarksCounter & 1) {
				// odd - thus quotemarks open
				$nextTokenUsePrefix = false;
				$newLine = null;
			} else {
				// even - thus quotemarks closed
				$nextTokenUsePrefix = true;
				$newLine = "\n";
			}

			if ($token === '{' || $token === '[') {
				$indent++;
				$result .= $token . $newLine;
			} elseif ($token === '}' || $token === ']') {
				$indent--;

				if ($indent >= 0) {
					$prefix = str_repeat($indString, $indent);
				}

				if ($nextTokenUsePrefix) {
					$result .= $newLine . $prefix . $token;
				} else {
					$result .= $newLine . $token;
				}
			} elseif ($token === ',') {
				$result .= $token . $newLine;
			} else {
				$result .= $prefix . $token;
			}
		}
		return str_replace('~~PRETTY_JSON_QUOTEMARK~~', '\"', $result);
	}
public static function prettyJson($json, $indString = "\t") {
		// Replace any escaped \" marks so we don't get tripped up on quotemarks_counter
		$tokens = preg_split('|([\{\}\]\[,])|', str_replace('\"', '~~PRETTY_JSON_QUOTEMARK~~', $json), -1, PREG_SPLIT_DELIM_CAPTURE);

		$indent = 0;
		$result = '';
		$quotemarksCounter = 0;
		$nextTokenUsePrefix = true;

		foreach ($tokens as $token) {
			$quotemarksCounter = $quotemarksCounter + (count(explode('"', $token)) - 1);

			if ($token === '') {
				continue;
			}

			if ($nextTokenUsePrefix) {
				$prefix = str_repeat($indString, $indent);
			} else {
				$prefix = null;
			}

			// Determine if the quote marks are open or closed
			if ($quotemarksCounter & 1) {
				// odd - thus quotemarks open
				$nextTokenUsePrefix = false;
				$newLine = null;
			} else {
				// even - thus quotemarks closed
				$nextTokenUsePrefix = true;
				$newLine = "\n";
			}

			if ($token === '{' || $token === '[') {
				$indent++;
				$result .= $token . $newLine;
			} elseif ($token === '}' || $token === ']') {
				$indent--;

				if ($indent >= 0) {
					$prefix = str_repeat($indString, $indent);
				}

				if ($nextTokenUsePrefix) {
					$result .= $newLine . $prefix . $token;
				} else {
					$result .= $newLine . $token;
				}
			} elseif ($token === ',') {
				$result .= $token . $newLine;
			} else {
				$result .= $prefix . $token;
			}
		}
		return str_replace('~~PRETTY_JSON_QUOTEMARK~~', '\"', $result);
	}
public static function prettyJson ( $ json , $ indString = "\t" ) { // Replace any escaped \" marks so we don't get tripped up on quotemarks_counter $ tokens = preg_split ( '|([\{\}\]\[,])|' , str_replace ( '\"' , '~~PRETTY_JSON_QUOTEMARK~~' , $ json ) , - 1 , PREG_SPLIT_DELIM_CAPTURE ) ; $ indent = 0 ; $ result = '' ; $ quotemarksCounter = 0 ; $ nextTokenUsePrefix = true ; foreach ( $ tokens as $ token ) { $ quotemarksCounter = $ quotemarksCounter + ( count ( explode ( '"' , $ token ) ) - 1 ) ; if ( $ token === '' ) { continue ; } if ( $ nextTokenUsePrefix ) { $ prefix = str_repeat ( $ indString , $ indent ) ; } else { $ prefix = null ; } // Determine if the quote marks are open or closed if ( $ quotemarksCounter & 1 ) { // odd - thus quotemarks open $ nextTokenUsePrefix = false ; $ newLine = null ; } else { // even - thus quotemarks closed $ nextTokenUsePrefix = true ; $ newLine = "\n" ; } if ( $ token === '{' || $ token === '[' ) { $ indent ++ ; $ result .= $ token . $ newLine ; } elseif ( $ token === '}' || $ token === ']' ) { $ indent -- ; if ( $ indent >= 0 ) { $ prefix = str_repeat ( $ indString , $ indent ) ; } if ( $ nextTokenUsePrefix ) { $ result .= $ newLine . $ prefix . $ token ; } else { $ result .= $ newLine . $ token ; } } elseif ( $ token === ',' ) { $ result .= $ token . $ newLine ; } else { $ result .= $ prefix . $ token ; } } return str_replace ( '~~PRETTY_JSON_QUOTEMARK~~' , '\"' , $ result ) ; }
Returns pretty JSON

@link https://github.com/ndejong/pretty_json/blob/master/pretty_json.php
@param string $json The original JSON string
@param string $indString The string to indent with
@return string
@deprecated Now there is a JSON_PRETTY_PRINT option available on json_encode()
Returns pretty JSON