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.
Unserialze written in PHP
Moderator: General Moderators
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
Unserialze written in PHP
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.
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.
Code: Select all
extract(unserialize($string));- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
That creates the variables I do NOT want the variables created in any way.Jenk wrote:?Code: Select all
extract(unserialize($string));
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Unserialze written in PHP
Your description is not very clear. Do you mean that given data like this that has been serialized: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.
Code: Select all
array( 'one' => 1, 'two' => 2, 'three' => 3, );one 1
two 2
three 3
(#10850)
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
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>";
?>Code: Select all
First element: $test['junk'] = "My test"
Second element: $test[0] = 56
Third element: $test['stuff']['mine'] = "Last one"- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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 />';
}- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
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.
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.
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.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
if further refine jshpro2's code: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.. 
Code: Select all
foreach (unserialize($foo) as $name => $value) {
echo $name .' => '.$value.'<br />';
}- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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.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.
(#10850)
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm