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
};
How can I deserialize this data in PHP?
P.S. I already have data in this format in DB, I cannot change it to json or another.
Since you stated you cannot change format:
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.
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);
Your result is:
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.
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.