如何转换这个
$myvar = '[aaa](11,22,33,44,55)[bbb](1,2,3,4,5)';
进入那个
$arr_name[1] = 'aaa';
$arr_val[1] = '11,22,33,44,55';
$arr_name[2] = 'bbb';
$arr_val[2] = '1,2,3,4,5';
我应该使用什么功能?
如何转换这个
$myvar = '[aaa](11,22,33,44,55)[bbb](1,2,3,4,5)';
进入那个
$arr_name[1] = 'aaa';
$arr_val[1] = '11,22,33,44,55';
$arr_name[2] = 'bbb';
$arr_val[2] = '1,2,3,4,5';
我应该使用什么功能?
if(preg_match_all('/\[(.*?)\]\((.*?)\)/',$myvar,$matches)) {
foreach($matches[0] as $k=>$v) {
$arr_name[$k] = $matches[1][$k];
$arr_val[$k] = $matches[2][$k];
}
}
您可以尝试使用正则表达式来提取您的键和变量,然后将它们插入到数组中:
$keys = array();
preg_match_all("/\[.+\]/", $myvar, $keys);
$values = array();
preg_match_all("/\(.+\)/", $myvar, $values);
$numKeys = count($keys);
$numVals = count($values);
$result = array();
foreach ($i = 0; $i < $numKeys; $i++) {
if ($i < $numVals) {
$result[trim($keys[$i], "[]")] = trim($values[$i], "()");
}
}