如何在PHP中反序列化Ruby序列化的YAML数据?

时间:2022-12-08 13:06:31

I'm a new guy in PHP and I want to deserialize Ruby serialized YAML data in PHP.

我是PHP的新手,我想在PHP中反序列化Ruby序列化的YAML数据。

Hash anyone done that yet?

Hash任何人都这样做了吗?

Ruby code:

Ruby代码:

class CustomField < ActiveRecord::Base
   serialize :possible_values
end

In Rails, I used the below code to retrieve data from a MySQL database:

在Rails中,我使用以下代码从MySQL数据库中检索数据:

  cf = CustomField.find(1).possible_values.to_s

and I can retrieve the result:

我可以检索结果:

  [\"文字\", \"スタイル\", \"入力チェック\"]

In cakephp, the retrieved result is:

在cakephp中,检索到的结果是:

--- 
- !binary |
  5paH5a2X

- !binary |
  44K544K/44Kk44Or

- !binary |
   5YWl5Yqb44OB44Kn44OD44Kv

1 个解决方案

#1


0  

Thanks a lot everybody.

非常感谢大家。

I resolved it myself. In rails, if you define serialize attribute like below

我自己解决了。在rails中,如果您定义如下所示的序列化属性

class CustomField < ActiveRecord::Base
  serialize :possible_values
end

Rails will save that serialized yaml and base64_encoded data into DB.

Rails会将序列化的yaml和base64_encoded数据保存到DB中。

So, In php

所以,在PHP中

First, I parsed yaml data that retrieve from db.

首先,我解析了从db检索的yaml数据。

$base64_encoded_possible_values = yaml_parse($result[0]["CustomField"]["possible_values"]);
/*
  array
  0 => 5paH5a2X,
  1 => 44K544K/44Kk44Or
  ...
*/

Before you can use yaml_parse method, you must install yaml php extension.

在使用yaml_parse方法之前,必须安装yaml php扩展。

In my case, I download the php_yaml.dll and put it into C:\xampp\php\ext, then add below line into C:\xampp\php\php.ini.

在我的情况下,我下载php_yaml.dll并将其放入C:\ xampp \ php \ ext,然后将以下行添加到C:\ xampp \ php \ php.ini中。

extension=php_yaml.dll

Second, decode that value.

第二,解码那个价值。

base64_decode($base64_encoded_possible_values[0]);//文字

#1


0  

Thanks a lot everybody.

非常感谢大家。

I resolved it myself. In rails, if you define serialize attribute like below

我自己解决了。在rails中,如果您定义如下所示的序列化属性

class CustomField < ActiveRecord::Base
  serialize :possible_values
end

Rails will save that serialized yaml and base64_encoded data into DB.

Rails会将序列化的yaml和base64_encoded数据保存到DB中。

So, In php

所以,在PHP中

First, I parsed yaml data that retrieve from db.

首先,我解析了从db检索的yaml数据。

$base64_encoded_possible_values = yaml_parse($result[0]["CustomField"]["possible_values"]);
/*
  array
  0 => 5paH5a2X,
  1 => 44K544K/44Kk44Or
  ...
*/

Before you can use yaml_parse method, you must install yaml php extension.

在使用yaml_parse方法之前,必须安装yaml php扩展。

In my case, I download the php_yaml.dll and put it into C:\xampp\php\ext, then add below line into C:\xampp\php\php.ini.

在我的情况下,我下载php_yaml.dll并将其放入C:\ xampp \ php \ ext,然后将以下行添加到C:\ xampp \ php \ php.ini中。

extension=php_yaml.dll

Second, decode that value.

第二,解码那个价值。

base64_decode($base64_encoded_possible_values[0]);//文字