Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hey guys i've got a string which looks like this:

b:Blue | y:Yellow | r:Red

and want to convert this string into an array with key value.

array(
  'b' => 'Blue',
  'y' => 'Yellow',
  'r' => 'Red'
);

I'm quite familiar with php except for explode stuff…

share|improve this question

closed as off-topic by HamZa, Second Rikudo, PeeHaa, hakre, hek2mgl Jul 24 '13 at 8:53

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist" – HamZa, Second Rikudo, hakre, hek2mgl
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Try something please, hints: explode() or preg_split() and you need array_combine(). –  HamZa Jul 24 '13 at 8:27
3  
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist –  Second Rikudo Jul 24 '13 at 8:29
1  
"I'm quite familiar with php except for" Good thing the community has created a very nice manual just for these type of situations. –  PeeHaa Jul 24 '13 at 8:29
1  
This question appears to be off-topic because it is about RTFM –  PeeHaa Jul 24 '13 at 8:29
1  
try splitting by '|' first.. then split again with ':' finally create an array based on those values.. good luck! –  Francis Fuerte Jul 24 '13 at 8:29

4 Answers 4

Instead of trying to split things, match it instead:

preg_match_all('/(\w):(\w+)/', 'b:Blue | y:Yellow | r:Red', $matches);
print_r(array_combine($matches[1], $matches[2]));

It matches an alphanumeric character, followed by a colon and then more alphanumeric characters; both parts are captured in memory groups. Finally, the first memory groups are combined with the second memory groups.

A more hacked approach is this:

parse_str(strtr('b:Blue | y:Yellow | r:Red', ':|', '=&'), $arr);
print_r(array_map('trim', $arr));

It turns the string into something that looks like application/x-www-form-urlencoded and then parses it with parse_str(). Afterwards, you need to trim any trailing and leading spaces from the values though.

share|improve this answer
    
Y U NO explain the codez :p –  HamZa Jul 24 '13 at 8:41
1  
@HamZa Was working on that ;-) –  Ja͢ck Jul 24 '13 at 8:42

explode first with | and after :

// I got the from php.net
function multiexplode ($delimiters,$string) {
    $ary = explode($delimiters[0],$string);
    array_shift($delimiters);
    if($delimiters != NULL) {
        foreach($ary as $key => $val) {
             $ary[$key] = multiexplode($delimiters, $val);
        }
    }
    return  $ary;
}
$string = "b:Blue | y:Yellow | r:Red"; // Your string

$delimiters = array(" | ", ":"); // | important that it is first
$matches = multiexplode($delimiters, $string);

/*
array (size=3)
  0 => 
    array (size=2)
      0 => string 'b' (length=1)
      1 => string 'Blue' (length=4)
  1 => 
    array (size=2)
      0 => string 'y' (length=1)
      1 => string 'Yellow' (length=6)
  2 => 
    array (size=2)
      0 => string 'r' (length=1)
      1 => string 'Red' (length=3)
*/
share|improve this answer

Try something like this:

$str = "b:Blue | y:Yellow | r:Red";

// Split on ' | ', creating an array of the 3 colors 
$split = explode(' | ', $str);

$colors = array();

foreach($split as $spl)
{
    // For each color now found, split on ':' to seperate the colors.
    $split2 = explode(':', $spl);

    // Add to the colors array, using the letter as the index. 
    // (r:Red becomes 'r' => 'red')
    $colors[$split2[0]] = $split2[1];
}

print_r($colors);

/*
Array
(
    [b] => Blue
    [y] => Yellow
    [r] => Red
)
*/
share|improve this answer
    
comments please comments ! –  HamZa Jul 24 '13 at 8:31
1  
@HamZa Added them. –  Richard A Jul 24 '13 at 8:34
$string = "b:Blue | y:Yellow | r:Red";
//split string at ' | '
$data = explode(" | ", $string);
$resultArray = array();
//loop through split result
foreach($data as $row) {
    //split again at ':'
    $result = explode(":", $row);
    //add key / value pair to result array
    $resultArray[$result[0]] = $result[1];
}
//print result array
print_r($resultArray);
share|improve this answer
    
If you ever write code (on SO), please add some comments. –  HamZa Jul 24 '13 at 8:29
    
@HamZa: Added some comments. But for someone who is familiar with php this shouldn't be a problem at all :) –  Tobias Kun Jul 24 '13 at 8:32
    
that's undoubtedly not true since this could be solved by just searching and reading the manual, and if it's true he must be lazy :) –  HamZa Jul 24 '13 at 8:34
    
@HamZa Back in high school (Would be college in the US I think) my teacher always said that well written code should not need comments or at least only few. But you make a good point. –  Richard A Jul 24 '13 at 8:36
    
@HamZa You have a point here :D Also i should not forget that there could be others reading this thread with not as much "experience" as the OP. –  Tobias Kun Jul 24 '13 at 8:38

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