Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP解析序列化数据?_Php_Parsing_Serialization - Fatal编程技术网

如何使用PHP解析序列化数据?

如何使用PHP解析序列化数据?,php,parsing,serialization,Php,Parsing,Serialization,这是我的序列化数据的一个示例: 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:

这是我的序列化数据的一个示例:

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";}
我希望能够用PHP解析并显示如下值:

  • 简介姓名:Ben
  • 个人资料\姓氏\权力
  • 国家概况:AL
我知道我需要像这样取消序列化:

$unserialize = unserialize($data);

但是我在用PHP解析数组时遇到了问题。我不断收到“为foreach()提供的参数无效”错误和不正确的数组输出。

这就是您要查找的内容

    <?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);   

输出:-

foreach循环的位置可能重复?转储
$unserialize
,检查它包含的内容并向我们显示您的代码您是否手动操作了该字符串?对于序列化PHP字符串,这是一个很大的禁忌。如果你不知道自己在做什么,它们就会被破坏,不能再被解除。
    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
    )