if I have a string of key-> value pairs in the following format:

MIME-Version: 1.0 From: "Tim Lincecum" Reply-To: "Tim Lincecum" Return-path: "Tim Lincecum" Content-Type: text/html; charset=iso-8859-1 Subject: Giants Win World Series!

How do I get an array such that arr['From'] = "Tim Lincecum", etc

I know there's the explode function, but the only delimiter I see (colon) is in the middle of a key and a value rather than between each pair. How can I approach this?

EDIT:

With zerkms' help suggesting there should actually be a newline, I came up with this:

    $arr = array();
    $temp = explode("\r\n",$data['headers']);
    foreach ($temp as $pair) {
        list($key,$value) = explode(': ',$pair);
        $arr[$key]=str_replace("\"","",$value);
    }
share|improve this question
1  
Where do you get this from? And why weren't headers separated with newline? – zerkms Dec 21 '10 at 0:35
This is how they are currently stored in our DB. – Lincecum Dec 21 '10 at 0:36
are you sure there is no newlines between parameters? – zerkms Dec 21 '10 at 0:36
You're right, I feel stupid :( Thanks – Lincecum Dec 21 '10 at 0:39
Does it have newlines between parameters? – ncuesta Dec 21 '10 at 0:45

3 Answers

up vote 2 down vote accepted

Since I did a good guess in comments - I think i need to repeat it here as an answer:

There is a newlines between parameters, so with

$parameters_pairs = explode("\r\n", $parameters_string);

you can split it into the name-value pairs, separated with colon.

share|improve this answer

You could always use regex :)

PHP

$str = 'MIME-Version: 1.0' . "\r\n" .
'From: "Tim Lincecum"' . "\r\n" . 
'Reply-To: "Tim Lincecum"' . "\r\n" . 
'Return-path: "Tim Lincecum"' . "\r\n" . 
'Content-Type: text/html; charset=iso-8859-1' . "\r\n" . 
'Subject: Giants Win World Series!';

preg_match_all('/(.*?):\s?(.*?)(\r\n|$)/', $str, $matches);

$headers = array_combine(array_map('trim', $matches[1]), $matches[2]);

var_dump($headers);

Output

array(6) {
  ["MIME-Version"]=>
  string(3) "1.0"
  ["From"]=>
  string(14) ""Tim Lincecum""
  ["Reply-To"]=>
  string(14) ""Tim Lincecum""
  ["Return-path"]=>
  string(14) ""Tim Lincecum""
  ["Content-Type"]=>
  string(29) "text/html; charset=iso-8859-1"
  ["Subject"]=>
  string(24) "Giants Win World Series!"
}

See it on IDEone.

share|improve this answer
Good, but not quite the right format. – Jonah Dec 21 '10 at 0:55
@Jonah Bron I imagine you are referring to just using \n as newline delimiter (I forget the spec). I have updated the answer. Thanks for the heads up. – alex Dec 21 '10 at 0:58
@alex: I was referring to to the fact that it wasn't in a key => value form as the OP requested. But you fixed it. – Jonah Dec 21 '10 at 1:09
@alex: I think you have to use \z, not $ in the regular expression. I think I tried it once... – Jonah Dec 21 '10 at 1:11
@Jonah Bron Maybe, but it worked in my example. Let me have a Google :) – alex Dec 21 '10 at 1:16
show 1 more comment
$temp = explode("\r\n", $string);
$sets = array();
foreach ($temp as $value) {
    $array = explode(': ', $value);
    $array[1] = trim($array[1], '"');
    $sets[$array[0]] = $array[1];
}

$string is the value you're getting from the database.

share|improve this answer
I was asking how to do string -> array. – Lincecum Dec 21 '10 at 0:43
Oh, you want it the other way around. Are they delimited by new lines? – Jonah Dec 21 '10 at 0:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.