在PHP中使用一个Perl的数据::自卸车格式阵列 [英] Use a Perl's Data::Dumper format array in PHP

查看:149
本文介绍了在PHP中使用一个Perl的数据::自卸车格式阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要存取权限阵列至极由Perl的数据::自卸车所输出的。结果
数组是原始文件,我需要用PHP来存取权限了。结果
数组被格式化为这样的:

I need to acces an array wich is outputed by Perl's Data::Dumper.
Array is in a raw file, and I need to acces it with php.
Array is formated as this:

$stats = {
  'days' => 25,
  'lengths' => {
    'Piwee' => 269,
    'Jeanne-' => 904,
    'kaAnar' => 340,
[... Very Big ARRAY....]

我发现就在这里这个解决方案,但我不想使用eval(),因为该数组将含有用户输入数据。
因为我总是需要eval()的输出在str_replace转换解决方案的情况下,我想我需要找到另一种方式来做到这一点。结果
也许转换成XML或直接PHP解析?结果
我不知道什么是最好的解决方案。

I found this solution on here, but i dont want to use eval() because this array will have user-input data. Since i will always need to eval() output in case of a str_replace solution, i think i need to find another way to do it.
Maybe convert to XML or by direct PHP parsing ?
I'm not sure about what would be the best solution here.

一个Perl / PHP / bash的解决方案会好起来的。

A perl / PHP / bash solution would be okay

注1:我不能编辑Perl的code的输出这个原始文件结果
注2:我不使用Perl code,所以我不知道如何与这样DATAS工作

note 1: I cant edit the Perl code that output this raw file
note 2: I dont code in Perl so i dont know how to work with such datas

推荐答案

不要使用数据::自卸车来导出数据。它主要用于调试的目的,不是为了数据序列化,特别是不能输入到一个陌生的语言

Don't use Data::Dumper to export your data. It's primarily for debugging purposes, not for data serialisation, and especially not for input to an alien language

使用Perl的 JSON 模块的连接code_json 调用创建Perl的哈希JSON字符串,和PHP的的 json_de code 把它转换成一个PHP关联数组

Use the Perl JSON module's encode_json call to create a JSON string from the Perl hash, and PHP's json_decode to convert it into a PHP associative array

例如,假设从数据::自卸车你的输出文件 stats.data ,再短像这样的Perl程序将打印JSON相当于STDOUT

For instance, say your output from Data::Dumper is in file stats.data, then a short Perl program like this will print the JSON equivalent to STDOUT

use strict;
use warnings qw/ all FATAL /;

use JSON;

print encode_json( do 'stats.data' or die $! ), "\n";

输出

{"lengths":{"Jeanne-":904,"Piwee":269,"kaAnar":340},"days":25}

这篇关于在PHP中使用一个Perl的数据::自卸车格式阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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