Perl正则表达式反序列化数据?

时间:2015-03-23 11:04:19

标签: php regex perl serialization

我面临与序列化数据相关的问题。我有如下序列化数据:

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;}}}

现在我想使用perl regex将这些数据反序列化为如下数组:

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 ) )

1 个答案:

答案 0 :(得分:2)

如果这是PHP serialize的输出,您可以使用以下内容来获取数据结构:

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

但是,通过添加换行符,空格和反斜杠,破坏了字符串。因此,您需要为这种独特的格式编写自定义解析器。