note 125699 added to function.serialize

From: Date: Wed, 20 Jan 2021 06:44:56 +0000
Subject: note 125699 added to function.serialize
Groups: php.notes 
Request: Send a blank email to php-notes+get-216198@lists.php.net to get a copy of this message
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/ ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.159.166 (proxied: 157.42.201.32) ---- Manual Page -- http://php.net/manual/en/function.serialize.php Edit -- https://master.php.net/note/edit/125699 Del: integrated -- https://master.php.net/note/delete/125699/integrated Del: useless -- https://master.php.net/note/delete/125699/useless Del: bad code -- https://master.php.net/note/delete/125699/bad+code Del: spam -- https://master.php.net/note/delete/125699/spam Del: non-english -- https://master.php.net/note/delete/125699/non-english Del: in docs -- https://master.php.net/note/delete/125699/in+docs Del: other reasons-- https://master.php.net/note/delete/125699 Reject -- https://master.php.net/note/reject/125699 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#216198) next »