<?php
    
if (isset($_GET["source"])) {
        
highlight_file(__FILE__);
        exit;
    }
    
    
// Since I'm *always* having trouble with json_decode, .. this function.
    // It returns a hashtable.
    
function json_freaking_decode($json$whitespace = array(" ""\t""\n""\r"), $control_char_stuff = array("("")""=")) {
        
$final_array = array();
        
$stack = array();
        
$var "";
        
$val "";
        
$mode "in-variable";
        
$quotes "";
        for (
$i 0$i strlen($json); $i++) {
            switch (
$json[$i]) {
                case 
"[":
                case 
"{":
                    if (!empty(
$var)) {
                        
array_push($stack$var);
                        
$var "";
                        
$mode "in-variable";
                    }
                    break;
                
                case 
"]":
                case 
"}":
                    if (
$mode == "in-value")
                        
$mode "in-variable";
                    
                    if (
count($stack) == 0// There are more closing brackets than opening ones - BUT WHO CARES :D - Just ignore it.
                        
break;
                    
                    if (!empty(
$var) && !empty($val)) {
                        
$tmp = array($var => $val);
                        foreach (
array_reverse($stack) as $layer) {
                            
$tmp = array($layer => $tmp);
                        }
                    }
                    else {
                        if (!empty(
$var)) { // {"val1", "val2"}
                            
$tmp $var// $var is the value. Yeah that's the kind of logic you get when parsing invalid json. Or perhaps it's valid, I don't know, it just needs to parse.
                            
foreach (array_reverse($stack) as $layer) {
                                
$tmp = array($layer => $tmp);
                            }
                        }
                    }
                    if (isset(
$tmp)) {
                        
$final_array array_merge_recursive($final_array$tmp);
                        unset(
$tmp);
                    }
                    
                    
$var "";
                    
$val "";
                    unset(
$stack[count($stack) - 1]);
                    break;
                
                case 
",":
                    if (
$quotes != "") {
                        if (
$mode == "in-variable")
                            
$var .= ",";
                        else
                            
$val .= ",";
                        break;
                    }
                    if (!empty(
$var) && !empty($val)) {
                        
$tmp = array($var => $val);
                        foreach (
array_reverse($stack) as $layer) {
                            
$tmp = array($layer => $tmp);
                        }
                    }
                    else {
                        if (!empty(
$var)) { // {"val1", "val2"}
                            
$tmp = array($var); // $var is the value. Yeah that's the kind of logic you get when parsing invalid json. Or perhaps it's valid, I don't know, it just needs to parse.
                            
foreach (array_reverse($stack) as $layer) {
                                
$tmp = array($layer => $tmp);
                            }
                        }
                    }
                    if (isset(
$tmp)) {
                        
$final_array array_merge_recursive($final_array$tmp);
                        unset(
$tmp);
                    }
                    
$var "";
                    
$val "";
                    
$mode "in-variable";
                    break;
                
                case 
"="// Whichever padawan wrote this has to learn a lot still.
                    
if ($quotes != "")
                        if (
$mode == "in-variable")
                            
$var .= "=";
                        else
                            
$val .= "=";
                    else {
                        
$val "";
                        
$mode "in-value";
                    }
                    break;
                
                case 
":":
                    if (
$quotes != "")
                        if (
$mode == "in-variable")
                            
$var .= ":";
                        else
                            
$val .= ":";
                    else {
                        
$val "";
                        
$mode "in-value";
                    }
                    break;
                
                case 
"'":
                    if (
$quotes == "'") {
                        
$quotes "";
                        if (
$mode == "in-value") {
                            if (!empty(
$var) && !empty($val)) {
                                
$tmp = array($var => $val);
                                foreach (
array_reverse($stack) as $layer) {
                                    
$tmp = array($layer => $tmp);
                                }
                            }
                            else {
                                if (!empty(
$var)) { // {"val1", "val2"}
                                    
$tmp = array($var); // $var is the value. Yeah that's the kind of logic you get when parsing invalid json. Or perhaps it's valid, I don't know, it just needs to parse.
                                    
foreach (array_reverse($stack) as $layer) {
                                        
$tmp = array($layer => $tmp);
                                    }
                                }
                            }
                            if (isset(
$tmp)) {
                                
$final_array array_merge_recursive($final_array$tmp);
                                unset(
$tmp);
                            }
                            
$var "";
                        }
                    }
                    else
                        if (
$quotes != "")
                            if (
$mode == "in-value")
                                
$val .= "'";
                            else
                                
$var .= "'";
                        else
                            
$quotes "'";
                    break;
                
                case 
'"':
                    if (
$quotes == '"') {
                        
$quotes "";
                        if (
$mode == "in-value") {
                            if (!empty(
$var) && !empty($val)) {
                                
$tmp = array($var => $val);
                                foreach (
array_reverse($stack) as $layer) {
                                    
$tmp = array($layer => $tmp);
                                }
                            }
                            else {
                                if (!empty(
$var)) { // {"val1", "val2"}
                                    
$tmp = array($var); // $var is the value. Yeah that's the kind of logic you get when parsing invalid json. Or perhaps it's valid, I don't know, it just needs to parse.
                                    
foreach (array_reverse($stack) as $layer) {
                                        
$tmp = array($layer => $tmp);
                                    }
                                }
                            }
                            if (isset(
$tmp)) {
                                
$final_array array_merge_recursive($final_array$tmp);
                                unset(
$tmp);
                            }
                            
$var "";
                        }
                    }
                    else
                        if (
$quotes != "")
                            if (
$mode == "in-value")
                                
$val .= '"';
                            else
                                
$var .= '"';
                        else
                            
$quotes '"';
                    break;
                
                default:
                    if (
$quotes == "" && (in_array($json[$i], $whitespace) || in_array($json[$i], $control_char_stuff)))
                        break;
                    
                    if (
$mode == "in-value") {
                        if (
$quotes != "") {
                            
$val .= $json[$i];
                        }
                        else {
                            if (
preg_match("/[0-9a-z]/i"$json[$i])) // hmm did someone forgot quotes around the value(s)?
                                
$val .= $json[$i];
                            else { 
// Let's guess that somebody forgot to comma-separate their values.
                                
if (!empty($var) && !empty($val)) {
                                    
$tmp = array($var => $val);
                                    foreach (
array_reverse($stack) as $layer) {
                                        
$tmp = array($layer => $tmp);
                                    }
                                }
                                else {
                                    if (!empty(
$var)) { // {"val1", "val2"}
                                        
$tmp = array($var); // $var is the value. Yeah that's the kind of logic you get when parsing invalid json. Or perhaps it's valid, I don't know, it just needs to parse.
                                        
foreach (array_reverse($stack) as $layer) {
                                            
$tmp = array($layer => $tmp);
                                        }
                                    }
                                }
                                if (isset(
$tmp)) {
                                    
$final_array array_merge_recursive($final_array$tmp);
                                    unset(
$tmp);
                                }
                                
$var $json[$i]; // "var:abc" the "a" would be ignored otherwise
                                
$val "";
                                
$mode "in-variable";
                            }
                        }
                    }
                    else
                        
$var .= $json[$i];
            }
        }
        
        if (!empty(
$var) && !empty($val)) {
            
$tmp = array($var => $val);
            foreach (
array_reverse($stack) as $layer) {
                
$tmp = array($layer => $tmp);
            }
        }
        else {
            if (!empty(
$var)) { // {"val1", "val2"}
                
$tmp = array($var); // $var is the value. Yeah that's the kind of logic you get when parsing invalid json. Or perhaps it's valid, I don't know, it just needs to parse.
                
foreach (array_reverse($stack) as $layer) {
                    
$tmp = array($layer => $tmp);
                }
            }
        }
        if (isset(
$tmp)) {
            
$final_array array_merge_recursive($final_array$tmp);
            unset(
$tmp);
        }
        
        if (!empty(
$_GET["json"]) && json_decode($json) == null)
            echo 
"PHP's json_decode function wouldn't have parsed your input. Just saying.\n\n";
        
        echo 
"My output:\n";
        
        return 
$final_array;
    }
    
    
?><pre>
DISCLAIMER: This function is weird. It just does something, and you'll have 
to deal with it. There is no 'expected behavior', the intended use for this 
is that you want to use some sort of json from somewhere but whoever wrote 
the api made it invalid. But instead of spending a lot of time making it 
valid or writing your own function to get your data out of it, you can just 
use this function. Or, that's the idea. No guarantees ;)

To parse JSON.. or whatever looks like it: ?json=your_json_data
To view the source: ?source

<?php 
    
    
if (isset($_GET["json"])) {
        
ob_start();
        
print_r(json_freaking_decode($_GET["json"]));
        
$data ob_get_contents();
        
ob_end_clean();
        echo 
htmlentities($data);
    }