Log in

View Full Version : String to Array function



hebs
05-26-2008, 01:53 PM
Does anybody know what function will turn a string into an array where key & value are contained in the string?

example:

$string = "LED: no LED\nCustom Text: \nText Position: Bottom Centre\nColor: Blue"

into array:

[LED] => no LED
[Custom Text] =>
[Text Position] => Bottom Centre
[Color] => Blue

Regards

Hebs

Master_script_maker
05-27-2008, 11:47 AM
<?php
function splitc($str,$d) {
preg_match_all("|([a-zA-Z0-9]+):[ ](.*)[".$d."]?|U",$str,$out, PREG_SET_ORDER);
for($i=0;$i<count($out);$i++) {
$arr[$out[$i][1]]=$out[$i][2];
}
return $arr;
}
to use:

$codes=splitc("code1: five;code2: 7;", ";");
or
$string=splitc("LED: no LED\nCustom Text: \nText Position: Bottom Centre\nColor: Blue", "\n");