note 75560 deleted from function.serialize by tularis
| From: | tularis@php.net | Date: | Fri, 08 Jun 2007 12:40:16 +0000 |
| Subject: | note 75560 deleted from function.serialize by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-127297@lists.php.net to get a copy of this message | ||
Note Submitter: emailfire at gmail dot com
Reason: useless
----
Here is a function I wrote that replicates the serialize function. Its pretty accurate but there may
be a few bugs or something that I have overlooked:
<?php
function serial($value) {
if (is_string($value)) {
return 's:'.strlen($value).':"'.$value.'";';
}
elseif (is_int($value)) {
return 'i:'.$value.';';
}
elseif (is_bool($value)) {
return 'b:'.(($value == true) ? 1 : 0).';';
}
elseif (is_array($value)) {
$a = 'a:'.count($value).':{';
foreach ($value as $key => $val) $a .= serial($key) . serial($val);
$a .= '}';
return $a;
}
elseif (is_object($value) && $name = get_class($value)) {
$o =
'O:'.strlen($name).':"'.strtolower($name).'":'.count($value).':{';
foreach ($value as $key => $val) $o .= serial($key) . serial($val);
$o .= '}';
return $o;
}
else {
return 'N;';
}
}
?>