Reputation: 1862
I have those values from my cookie:
a:2:{i:0;s:22:"302260 302250 302248";i:1;s:38:"Bruce Willis Jackie Chan Gary Oldman";}
I need this data to be separated like this:
$variable1 = [302260,302250,302248];
$variable2 = [Bruce Willis, Jackie Chan, Gary Oldman];
json_decode
returned NULL
, unserialize
returned:
array ( 0 => '302260 302250 302248', 1 => 'Bruce Willis Jackie Chan Gary Oldman')
Now I don't know how to explode the names to ["Bruce Willis", "Jackie Chan", "Gary Oldman"]
.
Upvotes: 0
Views: 92
Reputation: 491
You can try this with regex. Here is a simple example on how you could get it to work.
string = 'a:2:{i:0;s:20:"302260 302250 302248";i:1;s:36:"Bruce Willis Jackie Chan Gary Oldman";}';
$unserialized = unserialize($string);
// simple explode for the numbers
$variable1 = explode(" ", $unserialized[0]);
// here it gets harder, to get the result you could use some regex
// this regex will match a word+space+word. With preg_match_all you will get all those occurrences
preg_match_all("/[\w]+\s[\w]+/", $unserialized[1], $matches);
$variable2 = $matches[0];
var_dump($variable1);
var_dump($variable2);
If the names will not always consist out of two words separated by space, this could get a lot harder.
In this case you may can check it against some Database, if you have one that consists those values. Lets say, those values come from some stored user data, you could use that as a check.
If the names can be written upper/lower-case with any kind of character, you might be totally out of luck.
Imagine data like bOb lärson james baldwin jOhn franK frankenstein
. Could you see what names would belong together?
Even with a full name database, which consists all names ever, it will not be 100% accurate, as sometimes a surname can be used as a lastname as well.
Upvotes: 2