Created
November 28, 2013 08:54
-
-
Save xbill82/7688984 to your computer and use it in GitHub Desktop.
PHP: Robust JSON-decode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/** | |
* Custom version of the json_decode native function to allow comments in | |
* the config file. | |
*/ | |
function json_clean_decode($json, $assoc = false, $depth = 512, $options = 0) { | |
// search and remove comments like /* */ and // | |
$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json); | |
if(version_compare(phpversion(), '5.4.0', '>=')) { | |
$json = json_decode($json, $assoc, $depth, $options); | |
} | |
elseif(version_compare(phpversion(), '5.3.0', '>=')) { | |
$json = json_decode($json, $assoc, $depth); | |
} | |
else { | |
$json = json_decode($json, $assoc); | |
} | |
return $json; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment