例如,我有以下参数:
max_image_width=100,max_image_height=200,image_proportion=1.75
我想得到一个数组:
array('max_image_width'=>100,'max_image_height'=>200,'image_proportion'=175);
$str = 'max_image_width=100,max_image_height=200,image_proportion=1.75';
$cfg = parse_ini_string(
str_replace(',', "\n", $str)
);
print_r($cfg);
5.4
$a=[];foreach(explode(',',$i)as$b){$a[explode('=',$b)[0]]=explode('=',$b)[1];}
$output = array();
parse_str(str_replace(',', '&', 'max_image_width=100,max_image_height=200,image_proportion=1.75'), $output);
E.g. by using preg_match_all.
<?php
$t = 'max_image_width=100,max_image_height=200,image_proportion=1.75';
preg_match_all('!([^=]+)=([^,]+)!', $t, $m);
$x = array_combine($m[1], $m[2]);
var_export($x);
prints
array (
'max_image_width' => '100',
',max_image_height' => '200',
',image_proportion' => '1.75',
)
( though there are tons of other ways to do it without regular expressions ;-) )