I want to convert nested brackets to array with keywords. Here is pattern:
我想将嵌套括号转换为带关键字的数组。这是模式:
preg_match_all('/(?=\{((?:[^{}]++|\{(?0)\})++)\})/', $string, $res);
And data which need to parse:
以及需要解析的数据:
employee {
cashier { salary = 100; }
technician { age = 44; }
}
Result, that I need:
结果,我需要:
Array
(
[employee] => Array (
[0] => Array
(
[cashier] => Array
(
[salary] => 100
)
)
[1] => Array
(
[technician] => Array
(
[age] => 44
)
)
)
)
But cant iterate within nested brackets. Stucked here. Thanks in advance for your help
但是不能在嵌套括号内迭代。被困在这里在此先感谢您的帮助
1 个解决方案
#1
2
You'll need a recursive approach here.
你需要一个递归方法。
- First, analyze the outer structure with
{
and}
on both sides. - See, if we can find another nested structure
- If not, look for
key = value
pairs and return them
首先,用两侧的{和}分析外部结构。
如果我们能找到另一个嵌套结构,请参阅
如果没有,请查找key = value对并返回它们
A regex demo for the outer structure can be found on regex101.com, a complete PHP
demo would look as follows:
可以在regex101.com上找到外部结构的正则表达式演示,完整的PHP演示如下所示:
<?php
$string = <<<DATA
employee {
cashier { salary = 100; }
technician { age = 44; }
}
DATA;
// regular expressions
$outer = '~(?P<key>\w+)\s*(?P<value>\{(?:[^{}]*|(?R))*\})~';
// inner, key = value
$inner = '~(?P<key>\w+)\s*=\s*(?P<value>\w+)~';
function parse($string) {
global $outer, $inner;
$result = array();
// outer
preg_match_all($outer, $string, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$result[$match["key"]] = parse(
substr($match["value"], 1, -1)
);
}
// if not found, inner structure
if (!$matches) {
preg_match_all($inner, $string, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$result[$match["key"]] = $match["value"];
}
return $result;
}
return $result;
}
$result = parse($string);
print_r($result);
?>
This yields:
Array
(
[employee] => Array
(
[cashier] => Array
(
[salary] => 100
)
[technician] => Array
(
[age] => 44
)
)
)
#1
2
You'll need a recursive approach here.
你需要一个递归方法。
- First, analyze the outer structure with
{
and}
on both sides. - See, if we can find another nested structure
- If not, look for
key = value
pairs and return them
首先,用两侧的{和}分析外部结构。
如果我们能找到另一个嵌套结构,请参阅
如果没有,请查找key = value对并返回它们
A regex demo for the outer structure can be found on regex101.com, a complete PHP
demo would look as follows:
可以在regex101.com上找到外部结构的正则表达式演示,完整的PHP演示如下所示:
<?php
$string = <<<DATA
employee {
cashier { salary = 100; }
technician { age = 44; }
}
DATA;
// regular expressions
$outer = '~(?P<key>\w+)\s*(?P<value>\{(?:[^{}]*|(?R))*\})~';
// inner, key = value
$inner = '~(?P<key>\w+)\s*=\s*(?P<value>\w+)~';
function parse($string) {
global $outer, $inner;
$result = array();
// outer
preg_match_all($outer, $string, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$result[$match["key"]] = parse(
substr($match["value"], 1, -1)
);
}
// if not found, inner structure
if (!$matches) {
preg_match_all($inner, $string, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$result[$match["key"]] = $match["value"];
}
return $result;
}
return $result;
}
$result = parse($string);
print_r($result);
?>
This yields:
Array
(
[employee] => Array
(
[cashier] => Array
(
[salary] => 100
)
[technician] => Array
(
[age] => 44
)
)
)