-1

例如,我有以下参数:

max_image_width=100,max_image_height=200,image_proportion=1.75

我想得到一个数组:

array('max_image_width'=>100,'max_image_height'=>200,'image_proportion'=175);
4

4 回答 4

6
$str = 'max_image_width=100,max_image_height=200,image_proportion=1.75';
$cfg = parse_ini_string(
    str_replace(',', "\n", $str)
);

print_r($cfg);
于 2012-11-08T22:59:18.683 回答
3

5.4

$a=[];foreach(explode(',',$i)as$b){$a[explode('=',$b)[0]]=explode('=',$b)[1];}
于 2012-11-08T23:00:56.590 回答
3
$output = array();
parse_str(str_replace(',', '&', 'max_image_width=100,max_image_height=200,image_proportion=1.75'), $output);
于 2012-11-08T23:02:24.937 回答
2

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 ;-) )

于 2012-11-08T22:57:51.777 回答