pianoman993 Posted February 20, 2009 Share Posted February 20, 2009 Hello there PHP experts! I am trying to use a string like this: a:7:{s:10:"country_id";s:3:"226";s:5:"email";s:22:"email@wi.rr.com";s:10:"last_visit";s:19:"2009-02-20 10:22:58";s:7:"created";s:19:"2009-02-20 10:22:58";s:8:"modified";s:19:"0000-00-00 00:00:00";s:3:"URI";s:5:"/home";s:17:"FreakAuth_captcha";s:5:"sqqpi";} And be able to "explode" the data so I can get each value. So for instance I could fetch the country_id value by typing string_array['country_id']. is this possible? Any help is greatly appreciated! Thank you! - Pianoman993 Quote Link to comment https://forums.phpfreaks.com/topic/146160-solved-can-i-read-a-string-similar-to-explode/ Share on other sites More sharing options...
premiso Posted February 20, 2009 Share Posted February 20, 2009 This looks like a serialized array string. Why not simply unserialize it wham all data is back in an array. Quote Link to comment https://forums.phpfreaks.com/topic/146160-solved-can-i-read-a-string-similar-to-explode/#findComment-767358 Share on other sites More sharing options...
rhodesa Posted February 20, 2009 Share Posted February 20, 2009 i thought it was serialized too, but it wasn't...try this: <?php $code = 'a:7:{s:10:"country_id";s:3:"226";s:5:"email";s:22:"email@wi.rr.com";s:10:"last_visit";s:19:"2009-02-20 10:22:58";s:7:"created";s:19:"2009-02-20 10:22:58";s:8:"modified";s:19:"0000-00-00 00:00:00";s:3:"URI";s:5:"/home";s:17:"FreakAuth_captcha";s:5:"sqqpi";}'; $data = array(); preg_match_all('/s:\d+:"([^"]*)";s:\d+:"([^"]*)";/',$code,$matches); foreach($matches[1] as $n => $key){ $data[$key] = $matches[2][$n]; } print $data['country_id']; print_r($data); ?> Quote Link to comment https://forums.phpfreaks.com/topic/146160-solved-can-i-read-a-string-similar-to-explode/#findComment-767361 Share on other sites More sharing options...
rhodesa Posted February 20, 2009 Share Posted February 20, 2009 ah, it is serialized, but you removed the email address so when i unserialized it, it didn't work! <?php $code = 'a:7:{s:10:"country_id";s:3:"226";s:5:"email";s:15:"email@wi.rr.com";s:10:"last_visit";s:19:"2009-02-20 10:22:58";s:7:"created";s:19:"2009-02-20 10:22:58";s:8:"modified";s:19:"0000-00-00 00:00:00";s:3:"URI";s:5:"/home";s:17:"FreakAuth_captcha";s:5:"sqqpi";}'; $data = unserialize($code); print $data['country_id']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/146160-solved-can-i-read-a-string-similar-to-explode/#findComment-767365 Share on other sites More sharing options...
pianoman993 Posted February 20, 2009 Author Share Posted February 20, 2009 It definitely need to be unserialized. Thank you for your help. My code now works! Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/146160-solved-can-i-read-a-string-similar-to-explode/#findComment-767372 Share on other sites More sharing options...
premiso Posted February 20, 2009 Share Posted February 20, 2009 i thought it was serialized too, but it wasn't...try this: After some testing, there must be "whitespaces" in the email portion "s:22:"email@wi.rr.com" is not 22 characters. <?php $string = 'a:7:{s:10:"country_id";s:3:"226";s:5:"email";s:15:"email@wi.rr.com";s:10:"last_visit";s:19:"2009-02-20 10:22:58";s:7:"created";s:19:"2009-02-20 10:22:58";s:8:"modified";s:19:"0000-00-00 00:00:00";s:3:"URI";s:5:"/home";s:17:"FreakAuth_captcha";s:5:"sqqpi";}'; $arr = unserialize($string); print_r($arr); ?> It may work out of the box for him as the string may be formatted right, just not copied right. If it does not work, changing that email to be 15 makes it work just fine with the unserialize function. Interesting that it is that picky. EDIT: lol you found it out, oh well. I worked hard on finding that out, it deserved to be posted again Quote Link to comment https://forums.phpfreaks.com/topic/146160-solved-can-i-read-a-string-similar-to-explode/#findComment-767374 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.