PHP – split String in Key/Value pairs

If you don’t mind using regex …

$str = "key=value, key2=value2";
preg_match_all("/([^,= ]+)=([^,= ]+)/", $str, $r); 
$result = array_combine($r[1], $r[2]);
var_dump($result);

Leave a Comment