2

我正在尝试为我们的防火墙创建一个报告功能。防火墙规则以 json 格式存储。

这是字符串的示例。

[{"id":1,"enabled":true,"description":"TEMP","matchers":{"javaClass":"java.util.LinkedList","list":[{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"1.1.1.1","matcherType":"DST_ADDR"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"80","matcherType":"DST_PORT"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"1,3,2","matcherType":"DST_INTF"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"2.2.2.2","matcherType":"SRC_ADDR"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"1,2,wan","matcherType":"SRC_INTF"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"UDP,TCP,any","matcherType":"PROTOCOL"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"svrbjgu","matcherType":"DIRECTORY_CONNECTOR_USERNAME"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"intern-it","matcherType":"DIRECTORY_CONNECTOR_GROUP"}]},"ruleId":5001,"javaClass":"com.untangle.node.firewall.FirewallRule","block":false,"log":true}]

我有 preg_match_all 过滤器:

    preg_match_all('/\"description":"(.*?)\","matchers"/',$str,$description);
preg_match_all('/\"id":(.*?)\,"/',$str,$id);
preg_match_all('/\"ruleId":(.*?)\,"/',$str,$rule_id);
preg_match_all('/\"enabled":(.*?)\,"description"/',$str,$enable);
preg_match_all('/\"block":(.*?)\,"/',$str,$block);
preg_match_all('/\"log":(.*?)\}/',$str,$log);
preg_match_all('/\"value":"(.*?)\","matcherType":"DST_ADDR"/',$str,$dest_add);
preg_match_all('/\"value":"(.*?)\","matcherType":"DST_PORT"/',$str,$dest_port);
preg_match_all('/\"value":"(.*?)\","matcherType":"DST_INTF"/',$str,$dest_int);
preg_match_all('/\"value":"(.*?)\,","matcherType":"SRC_ADDR"/',$str,$src_add);
preg_match_all('/\"value":"(.*?)\","matcherType":"SRC_INTF"/',$str,$src_int);
preg_match_all('/\"value":"(.*?)\","matcherType":"PROTOCOL"/',$str,$protocol);
preg_match_all('/\"value":"(.*?)\","matcherType":"DIRECTORY_CONNECTOR_USERNAME"/',$str,$user);
preg_match_all('/\"value":"(.*?)\","matcherType":"DIRECTORY_CONNECTOR_GROUP"/',$str,$group);

发生的情况是每个匹配器的开始都是 VALUE":" 当我打印 $dest_port 我得到"1.1.1.1","matcherType":"DST_ADDR"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"80 ” 而不是只有 80 个。

任何有 preg_match_all 的人先找到 matchertype,然后将 backwords 读取到“value”:“?

任何帮助都会很棒!

4

2 回答 2

3
$xyz = <<<EOX
[{"id":1,"enabled":true,"description":"TEMP","matchers":{"javaClass":"java.util.LinkedList","list":[{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"1.1.1.1","matcherType":"DST_ADDR"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"80","matcherType":"DST_PORT"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"1,3,2","matcherType":"DST_INTF"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"2.2.2.2","matcherType":"SRC_ADDR"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"1,2,wan","matcherType":"SRC_INTF"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"UDP,TCP,any","matcherType":"PROTOCOL"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"svrbjgu","matcherType":"DIRECTORY_CONNECTOR_USERNAME"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"intern-it","matcherType":"DIRECTORY_CONNECTOR_GROUP"}]},"ruleId":5001,"javaClass":"com.untangle.node.firewall.FirewallRule","block":false,"log":true}]
EOX;

var_dump(json_decode($xyz)); //execute other operations, iteration etc here

这比正则表达式好得多。返回结构化数据,可以迭代。

于 2012-11-02T09:18:29.853 回答
0

最终与 json_decode 一起使用。

样本:

$jsondata ='[{"id":1,"enabled":true,"description":"TEMP","matchers":{"javaClass":"java.util.LinkedList","list":[{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"1.1.1.1","matcherType":"DST_ADDR"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"80","matcherType":"DST_PORT"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"1,3,2","matcherType":"DST_INTF"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"2.2.2.2","matcherType":"SRC_ADDR"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"1,2,wan","matcherType":"SRC_INTF"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"UDP,TCP,any","matcherType":"PROTOCOL"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"svrbjgu","matcherType":"DIRECTORY_CONNECTOR_USERNAME"},{"invert":false,"javaClass":"com.untangle.node.firewall.FirewallRuleMatcher","value":"intern-it","matcherType":"DIRECTORY_CONNECTOR_GROUP"}]},"ruleId":5001,"javaClass":"com.untangle.node.firewall.FirewallRule","block":false,"log":true}]';
$data = json_decode($jsondata,true);
echo '<table><tr><th WIDTH="10">Nr.</th><th WIDTH="10">RuleID</th><th WIDTH="150">Description</th><th WIDTH="100">Protocol</th><th WIDTH="75">Src Int</th><th WIDTH="150">Src Address</th><th WIDTH="100">Src Port</th><th WIDTH="75">Dest. Int</th><th WIDTH="150">Dest Address</th><th WIDTH="100">Dest. Port</th><th WIDTH="50">Action</th><th WIDTH="50">Log</th><th WIDTH="50">Enabled</th></tr>';
echo "<tr><td>test.</td><td> {$data[0]['ruleId']} </td><td>{$data[0]['description']}</td><td>{$data[0]['enabled']}</td></tr>" ; 
于 2012-11-02T10:14:16.600 回答