如何使用 PHP 解析序列化数据? [英] How can I parse serialized data with PHP?
本文介绍了如何使用 PHP 解析序列化数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的序列化数据的示例:
a:10:{s:7:"contact";s:1:"1";s:19:"profile_affiliation";s:23:"University, Inc.";s:18:"profile_first_name";s:3:"Ben";s:22:"profile_street_address";s:19:"8718 Tot Ave. S.";s:12:"profile_city";s:6:"Mobile";s:13:"profile_state";s:2:"AL";s:15:"profile_country";s:3:"USA";s:15:"profile_zipcode";s:5:"36695";s:18:"profile_home_phone";s:10:"2599494420";s:17:"profile_last_name";s:6:"权力";}
我希望能够用 PHP 解析它并显示如下值:
- profile_first_name:本
- profile_last_name:权力
- profile_state:AL
我知道我需要像这样反序列化它:
$unserialize = unserialize($data);
但是我在用 PHP 解析数组时遇到了问题.我不断收到为 foreach() 提供的无效参数"错误和不正确的数组输出.
解决方案
这就是你要找的
";print_r($original_array);
您的序列化字符串已损坏,因此您需要先修复它,然后反序列化
输出
数组([联系] =>1[profile_affiliation] =>大学,股份有限公司.[profile_first_name] =>本[profile_street_address] =>8718 Tot Ave. S.[profile_city] =>移动的[profile_state] =>AL[profile_country] =>美国[profile_zipcode] =>36695[profile_home_phone] =>2599494420[profile_last_name] =>权力)
This is an example of my serialized data:
a:10:{s:7:"contact";s:1:"1";s:19:"profile_affiliation";s:23:"University, Inc.";s:18:"profile_first_name";s:3:"Ben";s:22:"profile_street_address";s:19:"8718 Tot Ave. S.";s:12:"profile_city";s:6:"Mobile";s:13:"profile_state";s:2:"AL";s:15:"profile_country";s:3:"USA";s:15:"profile_zipcode";s:5:"36695";s:18:"profile_home_phone";s:10:"2599494420";s:17:"profile_last_name";s:6:"Powers";}
I want to be able to parse through it with PHP and display the values like so:
- profile_first_name: Ben
- profile_last_name: Powers
- profile_state: AL
I know I need to unserialize it like so:
$unserialize = unserialize($data);
but I'm having trouble parsing the array with PHP. I keep getting "Invalid argument supplied for foreach()" errors and the incorrect array output.
解决方案
This is what you are looking for
<?php
$serialized = 'a:10:{s:7:"contact";s:1:"1";s:19:"profile_affiliation";s:23:"University, Inc.";s:18:"profile_first_name";s:3:"Ben";s:22:"profile_street_address";s:19:"8718 Tot Ave. S.";s:12:"profile_city";s:6:"Mobile";s:13:"profile_state";s:2:"AL";s:15:"profile_country";s:3:"USA";s:15:"profile_zipcode";s:5:"36695";s:18:"profile_home_phone";s:10:"2599494420";s:17:"profile_last_name";s:6:"Powers";}';
$fixed = preg_replace_callback(
'/s:([0-9]+):"(.*?)";/',
function ($matches) { return "s:".strlen($matches[2]).':"'.$matches[2].'";'; },
$serialized
);
$original_array=unserialize($fixed);
echo "<pre>";
print_r($original_array);
You serailized string is corrupted so you need to repair it first then unserialize it
output
Array
(
[contact] => 1
[profile_affiliation] => University, Inc.
[profile_first_name] => Ben
[profile_street_address] => 8718 Tot Ave. S.
[profile_city] => Mobile
[profile_state] => AL
[profile_country] => USA
[profile_zipcode] => 36695
[profile_home_phone] => 2599494420
[profile_last_name] => Powers
)
Output:- https://eval.in/785908
这篇关于如何使用 PHP 解析序列化数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文