Thoman
Thoman

Reputation: 792

My PHP code serializes, but doesn't unserialize

THis this my code .

$data = array(
        '24 Jan|8:30' => '12.6',
        '22 Feb|8:30' => '250',
        '11 Mar|8:10' => '0',
        '31 Apr|23:30' => '7',
        '32 Apr|23:30' => '80',
        '33 Apr|23:30' => '67',
        '34 r|23:30' => '45',
        '35 Ap|23:30' => '66',
        '34 Lr|23:30' => '23',
        '3 Apr|23:30' => '23'
    );

    //echo serialize($data);
    $x = unserialize('a:10:{s:12:"24 Jan|8:30 ";s:4:"12.6";s:12:"22 Feb|8:30 ";s:3:"250";s:12:"11 Mar|8:10 ";s:1:"0";s:12:"31 Apr|23:30";s:1:"7";s:12:"32 Apr|23:30";s:2:"80";s:12:"33 Apr|23:30";s:2:"67";s:12:"34 r|23:30 ";s:2:"45";s:12:"35 Ap|23:30 ";s:2:"66";s:12:"34 Lr|23:30 ";s:2:"23";s:12:"3 Apr|23:30 ";s:2:"23";}');
    var_dump($x);

Not work in unserialize function. Please help!

Upvotes: 0

Views: 166

Answers (3)

Thomas Kelley
Thomas Kelley

Reputation: 10292

'a:10:{s:12:"24 Jan|8:30 ";s:4:"12.6";s:12:"22 Feb|8:30 ";s:3:"250";s:12:"11 Mar|8:10 ";s:1:"0";s:12:"31 Apr|23:30";s:1:"7";s:12:"32 Apr|23:30";s:2:"80";s:12:"33 Apr|23:30";s:2:"67";s:12:"34 r|23:30 ";s:2:"45";s:12:"35 Ap|23:30 ";s:2:"66";s:12:"34 Lr|23:30 ";s:2:"23";s:12:"3 Apr|23:30 ";s:2:"23";}'

...is not a valid serialization. Specifically, the s:12:"34 r|23:30 "; segment indicates that the string 34 r|23:30 contains 12 characters, which it does not.

Upvotes: 4

KingCrunch
KingCrunch

Reputation: 131841

The serialized representation of $data and the string you are trying to unserialize differ.

http://codepad.viper-7.com/3zlk1a

At offset 199 you see

s:12:"34 r|23:30 "

but the string (s) isn't 12 characters long (thats what s:12: mean). I guess something modified the serialized string directly. Just don't do it :) Always unserialize and work with the structured values.

Upvotes: 4

Flolink
Flolink

Reputation: 39

 $a = serialize($data);
 $x = unserialize($a);

Upvotes: -1

Related Questions