86 lines
2.1 KiB
PHP
86 lines
2.1 KiB
PHP
![]() |
<?php
|
||
|
|
||
|
// Pretty print some JSON
|
||
|
function json_format($json)
|
||
|
{
|
||
|
$tab = " ";
|
||
|
$new_json = "";
|
||
|
$indent_level = 0;
|
||
|
$in_string = false;
|
||
|
|
||
|
/*
|
||
|
commented out by monk.e.boy 22nd May '08
|
||
|
because my web server is PHP4, and
|
||
|
json_* are PHP5 functions...
|
||
|
|
||
|
$json_obj = json_decode($json);
|
||
|
|
||
|
if($json_obj === false)
|
||
|
return false;
|
||
|
|
||
|
$json = json_encode($json_obj);
|
||
|
*/
|
||
|
$len = strlen($json);
|
||
|
|
||
|
for($c = 0; $c < $len; $c++)
|
||
|
{
|
||
|
$char = $json[$c];
|
||
|
switch($char)
|
||
|
{
|
||
|
case '{':
|
||
|
case '[':
|
||
|
if(!$in_string)
|
||
|
{
|
||
|
$new_json .= $char . "\n" . str_repeat($tab, $indent_level+1);
|
||
|
$indent_level++;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$new_json .= $char;
|
||
|
}
|
||
|
break;
|
||
|
case '}':
|
||
|
case ']':
|
||
|
if(!$in_string)
|
||
|
{
|
||
|
$indent_level--;
|
||
|
$new_json .= "\n" . str_repeat($tab, $indent_level) . $char;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$new_json .= $char;
|
||
|
}
|
||
|
break;
|
||
|
case ',':
|
||
|
if(!$in_string)
|
||
|
{
|
||
|
$new_json .= ",\n" . str_repeat($tab, $indent_level);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$new_json .= $char;
|
||
|
}
|
||
|
break;
|
||
|
case ':':
|
||
|
if(!$in_string)
|
||
|
{
|
||
|
$new_json .= ": ";
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$new_json .= $char;
|
||
|
}
|
||
|
break;
|
||
|
case '"':
|
||
|
if($c > 0 && $json[$c-1] != '\\')
|
||
|
{
|
||
|
$in_string = !$in_string;
|
||
|
}
|
||
|
default:
|
||
|
$new_json .= $char;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return $new_json;
|
||
|
}
|