-4

I am facing issue related to serialized data. I have serialized data like below:

a:1:{i:1;a:3:{s:8:\"question\";s:18:\"What do you think?\";s:6:\"choice\";a:2:{i:1;s:3:\"Yes\";i:2;s:2:\"No\";}s:5:\"votes\";a:2:{i:1;i:1;i:2;i:0;}}}

Now I want to unserialize this data using perl regex to an array like below:

Array ( [question] => Who is going to be the Wild Cards in the AFC? [multi] => 1 [choice] => Array ( [1] => Cincinnati [2] => Jacksonville [3] => New York Jets [4] => Kansas City [5] => Denver [6] => Other ) [votes] => Array ( [1] => 0 [2] => 0 [3] => 1 [4] => 1 [5] => 0 [6] => 0 ) )
mpapec
  • 50,217
  • 8
  • 67
  • 127
Laeeq
  • 403
  • 1
  • 3
  • 14
  • 2
    How is your data serialised initially? It looks like it's trying to be something like JSON, but it isn't quite? – Sobrique Mar 23 '15 at 11:12
  • 2
    If you have any influence over how the data is serialized, make sure it's a known format with a Perl library to deserialize it, such as JSON. Regexes aren't a good fit her because of the recursion (the arbitrarily nestable brackets); Perl regexes can deal with that in principle but I don't find them easy enough to understand. – reinierpost Mar 23 '15 at 11:15
  • 2
    That doesn't look like valid JSON. – Sinan Ünür Mar 23 '15 at 11:18
  • 2
    Can you please provide the serialization corresponding to the input? You just showed two independent strings in different formats. – choroba Mar 23 '15 at 11:22
  • 1
    http://search.cpan.org/~bobtfish/PHP-Serialization-0.34/lib/PHP/Serialization.pm – mpapec Mar 23 '15 at 11:35
  • 1
    PHP::Serialization's `unserialize` doesn't work. It croaks on the line feeds and the backslashes. – ikegami Mar 23 '15 at 14:01
  • @ikegami so does php https://eval.in/303419 – mpapec Mar 23 '15 at 14:07
  • 1
    Thanks for ans. I have get it work by using PHP::Serialization' module. – Laeeq Mar 24 '15 at 09:12

1 Answers1

2

If this was the output of PHP's serialize, you could use the following to get a data structure:

use PHP::Serialization qw( unserialize );
my $data = unserialize($serialized);

However, the seralized string was mangled by the addition of line feeds, spaces and backslashes. As such, you will need to write a custom parser for this unique format.

ikegami
  • 367,544
  • 15
  • 269
  • 518