note 112412 added to function.json-decode

From: Date: Fri, 14 Jun 2013 02:11:26 +0000
Subject: note 112412 added to function.json-decode
Groups: php.notes 
Request: Send a blank email to php-notes+get-195204@lists.php.net to get a copy of this message
Thanks "1franck" for the json_clean_decode function to strip comments from the JSON! I only had one issue with it which was if there was a // comment at the very beginning of the JSON string (with no space before it). Here's my modified version that accounts for this (my only change was at the end of the regex): <?php /** Clean comments of json content and decode it with json_decode(). * Work like the original php json_decode() function with the same params * * @param string $json The json string being decoded * @param bool $assoc When TRUE, returned objects will be converted into associative arrays. * @param integer $depth User specified recursion depth. (>=5.3) * @param integer $options Bitmask of JSON decode options. (>=5.4) * @return string */ 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; } ?> ---- Server IP: 69.147.83.199 Probable Submitter: 67.250.10.9 ---- Manual Page -- http://php.net/manual/en/function.json-decode.php Edit -- https://master.php.net/note/edit/112412 Del: integrated -- https://master.php.net/note/delete/112412/integrated Del: useless -- https://master.php.net/note/delete/112412/useless Del: bad code -- https://master.php.net/note/delete/112412/bad+code Del: spam -- https://master.php.net/note/delete/112412/spam Del: non-english -- https://master.php.net/note/delete/112412/non-english Del: in docs -- https://master.php.net/note/delete/112412/in+docs Del: other reasons-- https://master.php.net/note/delete/112412 Reject -- https://master.php.net/note/reject/112412 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#195204) next »