Skip to content

Instantly share code, notes, and snippets.

@erikreagan
Created October 27, 2009 21:28
Show Gist options
  • Save erikreagan/219974 to your computer and use it in GitHub Desktop.
Save erikreagan/219974 to your computer and use it in GitHub Desktop.
Work with serialized data easier
#!/usr/bin/env php
<?php
// This command simply prints out the serialized data in a readable printed array format
// Found on a blog via a google search
// @see http://top-frog.com/2009/08/28/quickly-unserialize-data-in-textmate/
$data = unserialize(file_get_contents('php://stdin'));
print_r($data);
?>
#!/usr/bin/env php
<?php
// This command is the "re-serialization" version of my other gist
// I needed a way to get my newly edited data back to it's original serialized format so I wrote this
// First we create an array with each line (with the exception of the first and last)
$string_to_array = explode("\n",file_get_contents('php://stdin'));
// We cycle through the array and turn the data into a usable php array
foreach ($string_to_array as $line) {
if(preg_match("/(\s+)\[/", $line))
{
$key = preg_replace("/(\s+\[(.*)\].+)/","$2",$line);
$value = preg_replace("/.+(=> (.*)\n?)/","$2",$line);
$array_result[$key] = $value;
}
}
// Then we print it back out serialized and ready to be put back wherever it came from
echo serialize($array_result);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment