How to get array from serialized string in php? -
as result wordpress option got following details results. don't know how meaningful result following. can possible string parsing?
a:6:{s:14:"street_address";s:9:"add_line1";s:15:"street_address2";s:10:"add_line_2";s:9:"city_name";s:5:"cityname";s:5:"state";s:7:"statename";s:3:"zip";s:6:"999999";s:14:"country_select";s:2:"in";}
usually can use unserialize()
function.
$result = unserialize($your_serialized_string);
however there error in seralized string function return false error on log.
php notice: unserialize(): error @ offset 110 of 198 bytes in [...]
i've faced issue when string stored in database directly modified without using functions unserialize()
, serialize()
, because serialize function prepend length of value in array 's:14:"street_address"', 14 length of 'street_address', editing directly must edit length value of string edited.
the serialized string posted has 2 wrong values.
i solved following code, automatically regenerate wrong length values in string:
<?php @$result = unserialize($your_serialized_string); if(!$result){ // unserialize offset error $result_temp = preg_replace_callback('!s:(\d+):"(.*?)";!', function($match){ return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";'; }, $your_serialized_string); $result = unserialize($result_temp); } var_dump($result); echo $result['street_address']; ?>
you can access array associative array syntax
$result['street_address']
or object
$result = (object) $result; $result->street_address
Comments
Post a Comment