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@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

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.