Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fnicastri/2c5b399e8d290850e9ee06d66f6b44da to your computer and use it in GitHub Desktop.
Save fnicastri/2c5b399e8d290850e9ee06d66f6b44da to your computer and use it in GitHub Desktop.
/*
Matt Browne
The function by 1franck to allow comments works except if there is a comment at the very beginning of the file. Here's a modified version (only the regex was changed) that accounts for that.
*/
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