KItis
KItis

Reputation: 5626

PHP :taking sub strings from given string

I have string in the following format.

Option1:Option2:Option3:Option4

I want to get these items separated and put it into a array.

array(item1=>option1,item2=>option2,item3=>option3,item4=>option4)

etc.

is there a straight forward way to get this done using regular expressions with PHP.

Thanks in advance for sharing your experience with me.

Upvotes: 0

Views: 42

Answers (2)

ZenJ
ZenJ

Reputation: 309

You can use explode function for that

$array = explode(":", $str);

Upvotes: 1

Milad Naseri
Milad Naseri

Reputation: 4118

Use $arr = explode(":", $string); and then do this:

$result = array();
for ($i = 0; $i < count($arr); $i ++) {
    $result['item' . $i] = $arr[$i];
}

and you should find $result to be exactly what you want

Upvotes: 2

Related Questions