Page 1 of 2

Unserialze written in PHP

Posted: Mon Jan 23, 2006 6:21 pm
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.

Posted: Mon Jan 23, 2006 6:23 pm
by Jenk

Code: Select all

extract(unserialize($string));
?

Posted: Mon Jan 23, 2006 6:31 pm
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.

Posted: Mon Jan 23, 2006 6:35 pm
by feyd
maybe something like the set of functions that perform it in my post about session handlers (linked to from Useful Posts)

Re: Unserialze written in PHP

Posted: Mon Jan 23, 2006 7:06 pm
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

Posted: Mon Jan 23, 2006 7:28 pm
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.

Posted: Mon Jan 23, 2006 8:14 pm
by Christopher
Take a look at var_export() (also var_dump() and print_r() )

Posted: Mon Jan 23, 2006 8:22 pm
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 />';
}

Posted: Mon Jan 23, 2006 8:39 pm
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.

Posted: Mon Jan 23, 2006 9:50 pm
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.

Posted: Mon Jan 23, 2006 9:55 pm
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.. :?

Posted: Mon Jan 23, 2006 9:56 pm
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.

Posted: Mon Jan 23, 2006 9:57 pm
by josh
feyd, wouldn't that be semantically equivalent to my code? Maybe I missed something

Posted: Mon Jan 23, 2006 10:03 pm
by feyd
it is jshpro2, but it doesn't need the "temporary" variable to store the output from unserialize() foreach does that for you :)

Posted: Mon Jan 23, 2006 10:16 pm
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. :)