note 107270 added to function.preg-replace-callback
| From: | FlorianArndt | Date: | Thu, 26 Jan 2012 12:26:08 +0000 |
| Subject: | note 107270 added to function.preg-replace-callback | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185751@lists.php.net to get a copy of this message | ||
This small class allows PHP users to read JSON files with include statements in them. For instance
the include {{{ "relative/to/including.json" }}} is replaced by the content of the json
file located at "relative/to/including.json".
<?php
/**
* Handles JSON files with includes
* Purpose: handle bigger JSON files by featuring "includes"
*
* @author Florian Arndt
*/
class JWI {
/**
* Parses a JSON file and returns its contents
* @param String $filename
*/
static function read($filename) {
if(!file_exists($filename))
throw new Exception('<b>JWI Error: JSON file
<tt>'.$filename.'</tt> not found!</b>');
$content = join('', file($filename));
$dir = dirname($filename);
/**
* replace
* include statements
* with
* content of the file to include
* recursively
*/
$content = preg_replace_callback(
'/{{{\s*"\s*(.+)\s*"\s*}}}/', // >include file< - pattern
create_function(
'$matches', // callback parameter
sprintf(
'$fn = "%s/".$matches[1];'.
'return JWI::read($fn);',
realpath(dirname($filename))
)
),
$content
);
return $content;
}
}
----
Server IP: 212.124.37.9
Probable Submitter: 83.236.140.111
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace-callback.php
Edit -- https://master.php.net/note/edit/107270
Del: integrated -- https://master.php.net/note/delete/107270/integrated
Del: useless -- https://master.php.net/note/delete/107270/useless
Del: bad code -- https://master.php.net/note/delete/107270/bad+code
Del: spam -- https://master.php.net/note/delete/107270/spam
Del: non-english -- https://master.php.net/note/delete/107270/non-english
Del: in docs -- https://master.php.net/note/delete/107270/in+docs
Del: other reasons-- https://master.php.net/note/delete/107270
Reject -- https://master.php.net/note/reject/107270
Search -- https://master.php.net/manage/user-notes.php