note 125699 deleted from function.serialize by cmb

From: Date: Wed, 20 Jan 2021 08:58:10 +0000
Subject: note 125699 deleted from function.serialize by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-216200@lists.php.net to get a copy of this message
Note Submitter: Tricksndtips01 at gmail dot com ---- I have also written some code for importing serialized PHP data into PERL and then writing it back into PHP. I think the similar library posted above is actually more robust for a few select cases, but mine is more compact and a little easier to follow. I'd really like comments if anyone finds this useful or has improvements. Please credit me if you use my code. # serialize() # Takes a PERL scalar or properly formed (i.e. generated by unserialize() or # organized accordingly) referenced hash and converts it into a PHP format # serialized data string. Uses recursive algorithms for arrays and objects. sub serialize { my($input) = $_[0]; if ($input =~ /^\d+$/) { # ... Integer datatype return "i:$input;"; } elsif ($input =~ /^\d+\.\d+$/) { # ... Float datatype return "d:$input;"; } elsif (ref($input) eq 'HASH') { # ... Object and Array datatypes. # See serialize() above for explanation of the test used here to # distinguish Object from Array. if (exists(${$input}{'OBJECT'}) and not defined(${$input}{'OBJECT'})) { # ... Use a recursive solution for Objects my($className, $objLen, $objAssoc); $className = ${$input}{'name'}; $objLen = ${$input}{'len'}; # A little hacky; plunders the Array serialize logic $objAssoc = substr(serialize(${$input}{'assoc'}), 4); return "O:3:\"$className\":$objLen:$objAssoc"; } else { # ... Use a recursive solution for Arrays my($key, $value, $serKey, $serVal, $hashLen); my($serHash) = ''; while (($key, $value) = each(%{$input})) { $serKey = serialize($key); $serVal = serialize($value); $serHash = $serHash . $serKey . $serVal; } $hashLen = scalar(keys(%{$input})); return "a:$hashLen:{$serHash}"; } } else { # ... String datatype my $len = length($input); return "s:$len:\"$input\";"; } } If you have any problem then you can contact me by going here: https://tricksndtips.com/contact-us/

« previous php.notes (#216200) next »