PHP 8.3.21 Released!

Voting

: min(one, nine)?
(Example: nine)

The Note You're Voting On

Florian Arndt
13 years ago
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;
}
}

<< Back to user notes page

To Top