parse string to array

Discussion in 'PHP' started by bumbarov, Mar 23, 2012.

  1. #1
    Hello!

    Is there any short method for parsing a string to an array?

    $str = " 'red','green','blue' ";

    I want to receive the following:

    $arr = array ('red', 'green', 'blue');

    Thank you!
     
    bumbarov, Mar 23, 2012 IP
  2. Devitor

    Devitor Peon

    Messages:
    57
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    0
    #2
    explode

    $str = " 'red','green','blue' ";
    $arr = explode(",", $str);
    Code (markup):
     
    Devitor, Mar 23, 2012 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    $str = " 'red','green','blue' ";
    
    $arr = array_map('trim', explode(',', str_replace("'", '', $str)));
    PHP:
    That should work, assuming the original string is in that format...
     
    danx10, Mar 24, 2012 IP