我有这个字符串
[1: boy, 2: girl]
我想把它转换成阵列
1=>boy
2=>girl
Hi您可以使用爆炸来获得所需的输出
$tp = '[1: boy, 2: girl]';
$tp = trim($tp,'[]');
$new = array();
foreach(explode(',',$tp) as $each_elem){
$temp = explode(':',$each_elem);
$new[trim($temp[0])] = $temp[1];
}
试试这个:
$str = "[5: boy, 8: girl]";
$exps = preg_split('/'W/',$str, 0,PREG_SPLIT_NO_EMPTY);
$size = count($exps);
for($i=0; $i<$size; $i++)
$array[$exps[$i]] = $exps[++$i];
输出:
5=>boy
8=>girl
简单正则表达式解决方案:
preg_match_all('/('d+): ([^,']]+)/', $string, $matches);
$array = array_combine($matches[1], $matches[2]);