note 46017 deleted from function.serialize by tomsommer
| From: | tomsommer@php.net | Date: | Sun, 26 Sep 2004 22:47:58 +0000 |
| Subject: | note 46017 deleted from function.serialize by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-77418@lists.php.net to get a copy of this message | ||
Note Submitter: joe at no dot spam dot please dot kybert dot co dot uk
----
Further to the code posted here by goetas at lignano dot it - GoetAs, I needed a function that could
safely serialize both arrays and strings.
In an array, only the value is encoded, not the key text (TODO?). The resultant output from any
serialize call is also base64 encoded for completeness.
<?php
function base64_unserialize($data)
{
$data2 = base64_decode($data);
$data = unserialize($data2);
if( is_array($data) )
{
$ret = array();
foreach ($data as $k => $v)
{
$ret[$k] = base64_unserialize($v);
}
}
else
{
$ret = base64_decode($data);
}
return $ret;
}
function base64_serialize($data)
{
if( is_array($data) )
{
$ret = array();
foreach ($data as $k => $v)
{
$ret[$k] = base64_serialize($v);
}
}
else
{
$ret = base64_encode($data);
}
$ret2 = serialize($ret);
$ret = base64_encode($ret2);
return $ret;
}
?>