如何在PHP中反序列化Perl Data :: Dumper输出 [英] How to deserialize Perl Data::Dumper output in PHP

查看:126
本文介绍了如何在PHP中反序列化Perl Data :: Dumper输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Perl中有一个导出变量的结果,像这样的字符串:

I have a result of export variable in Perl like this string:

$VAR1 = {
    'guard' => undef,
    'work_hand' => undef,
    'images' => 
        {'1' => 
            {
            'mini_height' => 150,
            'width' => 150,
            'extension' => 'jpg',
            'filename' => 'object_1.1330907414.96873.jpg',
            'mini_width' => 150,
            'class' => 'Ontico::Image',
            'height' => 150,
            'mini_filename' => 'object_1.1330907414.96873.mini.jpg',
            'size' => 26053,
            'symname' => 'big_logo'
            },
        '2' => 
            {
            'width' => 48,
            'extension' => 'jpg',
            'alt' => 'Даниэле Галлоппа',
            'height' => 48,
            'mini_filename' => 'object_91.1235312905.mini.jpg',
            'size' => 12809,
            'symname' => 'logo',
            'mini_height' => 150,
            'filename' => 'object_91.1235312905.jpg',
            'mini_width' => 150,
            'class' => 'Ontico::Image'
            }
        },
        'show_league_banner' => 0,
        'back_hand' => undef,
        'weight_category' => undef,
        'stick_position' => undef
    };

如何在PHP中反序列化此数据?

How can I deserialize this data in PHP?

P.S.我已经在数据库中使用这种格式的数据,无法将其更改为json或其他格式.

推荐答案

由于您声明无法更改格式,所以:

我不喜欢使用eval,但由于您的语法 so 接近预期的PHP数组语法,所以我认为我们可以让它滑动.

I don't like using eval, but because your syntax is so close to the expected PHP array syntax, I think we can let it slide.

$string设置为等于数据库中符合以下格式的内容.有关使用您提供的数据的工作示例,请参见下文.最终,PHP会在perl var的开头将变量设置为新的已解析数组.

Set $string equal to the contents from your database that fits the format below. See below for a working example using the data you provided. At the end of the day, PHP will set the variable at the beginning of your perl var to the new parsed array.

由于它将是文本块/大字符串,所以请执行以下操作:

Since it is going to be a textblock/large string, do:

<?php
$string = "\$VAR1 = {
    'guard' => undef,
    'work_hand' => undef,
    'images' =>
        {'1' =>
            {
            'mini_height' => 150,
            ... // truncated for readability
    };";

$res = str_replace(array("{", "}", 'undef'), array("array(", ")", "''"), $string);

eval($res);

print_r($VAR1);

您的结果是:

Array
(
    [guard] =>
    [work_hand] =>
    [images] => Array
        (
            [1] => Array
                (
                    [mini_height] => 150 ...

注意:我建议您现在花些时间将数据库内容升级和升级为更标准的格式,原因是将来将更易于维护.

Note: I would suggest you take the time now to retrofit and upgrade your database content to a more standard format simply for the fact that it will be easier to maintain in the future.

您可以遍历数据库,逐行抓取所有内容,然后将数据运行到上面的函数中,并将其包装在json_encode()中,并使用新的JSON字符串更新数据库行.这将使您免于以后的麻烦,并使您可以为新标准更新所有数据.

You can loop through your database, grab all the contents row by row, at which point you run the data into the function above, and wrap it in json_encode() and update your database row with the new JSON string. This will save you a headache in the future and allow you to update all your data for the new standard.

这篇关于如何在PHP中反序列化Perl Data :: Dumper输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆