2012-11-02 159 views
2

我正在嘗試爲我們的防火牆創建一個報告功能。 防火牆規則以json格式存儲。PHP preg_match_all/preg_match

這是一個字符串示例。

[{"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"}, { 「反轉」:假的, 「javaClass」:「com.untangle.node.firewall.FirewallRuleMatcher」,「value」:「80」,而不僅僅是80.

任何一個有preg_match_all的人先找到matchertype,然後讀回「值「:」?

任何幫助將是太棒了!

+2

爲什麼不* *解析JSON的用JSON解析器...? – deceze

+1

使用[** json_decode **](http://www.php.net/manual/en/function.json-decode.php)。 –

+0

我應該可以使用json_decode,但它會給我一些我不需要的信息,所以我仍然需要將某些部分修正。 它可能easyer只是在json_decode輸出上應用過濾器。 如果我有時間,我會用它進行一些測試;) – WebFooL

回答

2
$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 

這比正則表達式更好。返回結構化數據,您可以遍歷。

+0

+1。 @WebFoot,使用'json_decode()'! – Nadh

+1

我已經結束了運行json_decode。 然後從那裏操縱數組。 謝謝大家! – WebFooL

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>" ;