note 42483 deleted from function.var-export by tomsommer

From: Date: Tue, 18 May 2004 22:32:00 +0000
Subject: note 42483 deleted from function.var-export by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-69806@lists.php.net to get a copy of this message
Note Submitter: Haravikk ---- I created this little function to output a variable of any kind (including arrays) in such a format that they can be saved and later included by PHP to immediately gain access to the now cached variable. It supports nested arrays through the recursive nature of it. Note that $phpEx is the extension being used by PHP, and $rootdir is a path to the root directory from which the script is operating, they aren't really necessary but many sites could require them. <?php function cache_variable ($variable, $name) { global $phpEx, $rootdir; // A variable called $name has been passed to us for caching as a file of the same name, create // the file header $file = "<?php\n"; $file .= "if (!defined('SECURE')) die ('Hacking attempt');\n"; $content = cache_variable_recur ($variable); $file .= "$$name = $content;\n"; // Finish the file $file .= '?>'; $handle = fopen ($rootdir . 'cache/' . $name.$phpEx, 'w'); fwrite ($handle, $file); fclose ($handle); } function cache_variable_recur ($input) { if (is_array ($input)) { $content .= 'array ('; foreach ($input as $k => $v) { $parts[] = "'$k' => " . cache_variable_recur ($v); } $content .= implode (', ', $parts).')'; } else { $input = addcslashes ($input, "'"); $content .= "'$input'"; } return $content; } ?>

« previous php.notes (#69806) next »