note 51078 added to function.var-export
| From: | roman at DIESPAM dot feather dot org dot ru | Date: | Fri, 18 Mar 2005 21:46:32 +0000 |
| Subject: | note 51078 added to function.var-export | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-86666@lists.php.net to get a copy of this message | ||
Function that exports variables without adding any junk to the resulting string:
<?php
function encode($var){
if (is_array($var)) {
$code = 'array(';
foreach ($var as $key => $value) {
$code .= "'$key'=>".encode($value).',';
}
$code = chop($code, ','); //remove unnecessary coma
$code .= ')';
return $code;
} else {
if (is_string($var)) {
return "'".$var."'";
} elseif (is_bool($code)) {
return ($code ? 'TRUE' : 'FALSE');
} else {
return 'NULL';
}
}
}
function decode($string){
return eval('return '.$string.';');
}
?>
The resulting string can sometimes be smaller, that output of serialize(). May be useful for storing
things in the database.
----
Manual Page -- http://www.php.net/manual/en/function.var-export.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51078
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51078&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51078&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51078&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51078&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51078&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51078&report=yes
Search -- http://master.php.net/manage/user-notes.php