Jump to content

[SOLVED] Can I read a string similar to explode() ?


pianoman993

Recommended Posts

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 protected]";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

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 protected]";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);
?>

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 protected]";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'];
?>

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 protected]" is not 22 characters.

 

<?php
$string = 'a:7:{s:10:"country_id";s:3:"226";s:5:"email";s:15:"[email protected]";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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.