有人可以帮忙解决这个注册问题吗?
我想获得此示例文本的所有组件:
filter:bundesland|4-berlin|6-hamburg|3-nordrhein-westphalen:stadt|3-koeln|2-dresden:typ|5-schule|2-kindergarten|6-hort
我使用以下方法匹配此字符串:
filter(:(bundesland|stadt|typ)(\|\d-[a-z\s-]*)*)*
我需要得到这样的结果:
Array
(
['bundesland'] => Array
(
[1] => 4
[2] => 6
[3] => 3
)
['stadt'] => Array
(
[1] => 3
[2] => 2
)
['typ'] => Array
(
[1] => 5
[2] => 2
[3] => 6
)
)
(我不关心标签。只需要ID)
答案 0 :(得分:4)
你可以试试这个:
$pattern = '~(?:(?:filter|\G(?!\A)):(\w+)\||\G(?!\A))(\d+)[^\d:\s]*~';
$result = array();
if (preg_match_all($pattern, $str, $matches, PREG_SET_ORDER)) {
foreach ($matches as $m) {
if (!empty($m[1])) $current = $m[1];
$result[$current][] = $m[2];
}
}
print_r($result);
答案 1 :(得分:2)
您可以使用此PHP代码:
$s = 'filter:bundesland|4-berlin|6-hamburg|3-nordrhein-westphalen:stadt|3-koeln|2-dresden:typ|5-schule|2-kindergarten|6-hort';
$result = array();
preg_replace_callback('~:(\w+)([^:]+)~', function ($m) use (&$result) {
preg_match_all('/(?<=\|)[0-9]+/', $m[2], $sm); $result[$m[1]] = $sm[0]; }, $s);
print_r($result);
Array
(
[bundesland] => Array
(
[0] => 4
[1] => 6
[2] => 3
)
[stadt] => Array
(
[0] => 3
[1] => 2
)
[typ] => Array
(
[0] => 5
[1] => 2
[2] => 6
)
)