Unserialze written in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Unserialze written in PHP

Post by AKA Panama Jack »

Yes, I know there is an unserialize command in PHP so don't post about it. :)

What I am looking for is a program that will take a serialized string and break it down into a list of variable names with associated values. I have been looking all over the net for a code snippet that will do this but haven't been able to find anything and don't have the time to create one from scratch.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

extract(unserialize($string));
?
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Jenk wrote:

Code: Select all

extract(unserialize($string));
?
That creates the variables I do NOT want the variables created in any way.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

maybe something like the set of functions that perform it in my post about session handlers (linked to from Useful Posts)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Unserialze written in PHP

Post by Christopher »

AKA Panama Jack wrote:What I am looking for is a program that will take a serialized string and break it down into a list of variable names with associated values.
Your description is not very clear. Do you mean that given data like this that has been serialized:

Code: Select all

array( 'one' => 1, 'two' => 2, 'three' => 3, );
You could get data like:

one 1
two 2
three 3
(#10850)
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Code: Select all

<?
$test = array();

$test['junk'] = "My test";
$test[0] = 56;
$test['stuff']['mine'] = "Last one";

$serialized_data = serialize($test);

$unserialized_list = unserialize_function($serialized_data); // this is a function that converts the serialised string into an array of what each variable in the array actually is

echo "First element: " . $unserialized_list[0] . "<br>";
echo "Second element: " . $unserialized_list[1] . "<br>";
echo "Third element: " . $unserialized_list[2] . "<br>";
?>
The output you would see is...

Code: Select all

First element: $test['junk'] = "My test"
Second element: $test[0] = 56
Third element: $test['stuff']['mine'] = "Last one"
Like I said it doesn't actually create the variables and populate them but creates a list of all the variables and the content from the serialized string.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Take a look at var_export() (also var_dump() and print_r() )
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

What's wrong with just creating the variables into an array?

Code: Select all

$myarray['name'] = 'value';

$otherarray  = unserialize(serialize($myarray));
echo 'Unserialized string:<br />';
foreach ($otherarray as $name => $value) {
echo $name .' => '.$value.'<br />';
}
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

What you guys don't understand is that the variables MUST NOT BE CREATED. Unserialise will create a variable array. What I need requires that the variable array not be created. Period. The serialized text should be converted into a human readable form of text as I indicated above without the actual variables being created.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Why specifically can't they be created?

With the method I posted above the variables are only created in a temporary array so you can loop over that temp array and extract the values into a human readable form

If you want to re-invent the wheel it'd be pretty easy, until you get into escaping things, etc... it will become a real PITA

You'd have to take the data you want serialized and check the type of the variable, the length, etc... Even then what do you plan on doing inside your version of unserialize. Will your version of unserialize just output the data without storing it in any kind of variable? Unless you plan on outputting your data immediatly you are going to need to store it in an array of some sort.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if further refine jshpro2's code:

Code: Select all

foreach (unserialize($foo) as $name => $value) {
echo $name .' => '.$value.'<br />';
}
provided the original was an array, that code works.. and it's not hard to check if the original was an array if you understand the serializing notation.. :?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

AKA Panama Jack wrote:What you guys don't understand is that the variables MUST NOT BE CREATED. Unserialise will create a variable array. What I need requires that the variable array not be created. Period. The serialized text should be converted into a human readable form of text as I indicated above without the actual variables being created.
I got that. I think var_export(unserialize($data)) gets close. I think you may need to parse serialized data manually. Check phpclasses.org or some of the scripts sites.
(#10850)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

feyd, wouldn't that be semantically equivalent to my code? Maybe I missed something
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it is jshpro2, but it doesn't need the "temporary" variable to store the output from unserialize() foreach does that for you :)
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Everyone IGNORED the fact it should NOT use the unserialize function from PHP in any fashion. :(

Even after I posted...
AKA Panama Jack wrote:Yes, I know there is an unserialize command in PHP so don't post about it. :)
Locked