我试图开始使用preg_match,但我无法获得正确的模式。
该字符串看起来像[abc] def [ghi] jul [mno]
pqr。
我需要array(abc => def, ghi => jul, mno => pqr)
之类的东西。
有什么想法吗?
尝试此regex
/'[([a-z]+)']( [a-z]+)?/
在preg_match_all()
中
之后尝试
$regex = '/'[([a-z]+)'][ ]?([a-z]+)?/';
$string = '[abc] def [ghi] jul [mno] pqr';
preg_match_all($regex, $string, $matches);
$arr = array();
foreach($matches[1] as $index => $match){
$arr[$match] = $matches[2][$index];
}
print_r($arr);
你可以为$matches[2][$index]
添加isset()
,但我认为我的代码也可以。
@MateiMihai建议$result = array_combine($matches[1], $matches[2]);