PHP: error: Notice: unserialize(): Error at offset 438 of 750 bytes in

I tried to unserialize an serialized string:

$sri = 'a:8:{s:3:"key";s:32:"73b43c7df3604c8d725f856b219cffda";s:5:"email";s:19:"Aligh.Pro@gmail.com";s:4:"date";s:0:"";s:9:"user_info";a:4:{s:2:"id";i:2;s:5:"email";s:19:"Aligh.Pro@gmail.com";s:10:"first_name";s:5:"mahdi";s:8:"discount";s:4:"none";}s:9:"downloads";a:1:{i:0;a:3:{s:2:"id";i:44;s:8:"quantity";i:1;s:7:"options";a:2:{s:8:"quantity";i:1;s:8:"price_id";s:1:"1";}}}s:12:"cart_details";a:1:{i:0;a:10:{s:4:"name";s:17:"?????? ??";s:2:"id";i:44;s:11:"item_number";a:3:{s:2:"id";i:44;s:8:"quantity";i:1;s:7:"options";a:2:{s:8:"quantity";i:1;s:8:"price_id";s:1:"1";}}s:10:"item_price";d:20000;s:8:"quantity";i:1;s:8:"discount";d:0;s:8:"subtotal";d:20000;s:3:"tax";d:0;s:4:"fees";a:0:{}s:5:"price";d:20000;}}s:4:"fees";a:0:{}s:8:"currency";s:4:"RIAL";}';
$data_array = unserialize((string) $sri);
echo '<pre>' . $data_array;
print_r($data_array);
echo '</pre>';

but I saw this error: Notice: unserialize(): Error at offset 438 of 750 bytes in ...

I tried above serialized string by online unserialization. The string worked well. but in my cumputer, it doesn't.

what's the problem?

1 answer

  • answered 2018-01-14 13:43 Transitive

    Baba already wrote a great answer in this post. If you are in need of a quick fix, this code will do the job.

    $data = preg_replace_callback(
        '!s:(\d+):"(.*?)";!', 
        function($m) { 
            return 's:'.strlen($m[2]).':"'.$m[2].'";'; 
        }, 
        $sri);
    
    var_dump(unserialize($data));