0

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"].

2
  • 4
    You will have a problem with the actor names : if any of them is composed of more than three words, no algorithm will be able to separate them properly. I suggest manually adding the commas. Commented Sep 28, 2017 at 19:15
  • You should look into preg_match() and then use explode() Commented Sep 28, 2017 at 19:18

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I really appreciate the reference to bOb lärson james baldwin jOhn franK frankenstein. He's always been one of my favorite actors.
@Don'tPanic well yes, I agree. Always roll with the classics. So everyone know what I intend to mean with my examples.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.