3

次の内容のファイルを解析して表に表示しようとしています。問題は、最初のリース スタンザしか取得できないことです。私はそれらの残りを取得する方法がわかりませんか?何か案は?

入力:

# The format of this file is documented in the dhcpd.leases manual page.
# This lease file was written by isc-dhcp-V3.0.3b1

lease 192.168.98.25 {
  starts 5 2012/10/05 21:18:41;
  ends 5 2012/10/05 22:48:15;
  tstp 5 2012/10/05 22:18:41;
  binding state free;
  hardware ethernet 78:d6:f0:c1:7f:b5;
  uid "\001x\326\360\301\177\265";
}
lease 192.168.10.1 {
  starts 5 2012/10/04 12:23:23;
  ends 5 2012/10/05 22:48:15;
  binding state active;
  next binding state free;
  hardware ethernet 56:a3:f0:d1:75:b5;
  uid "\001x\326\360\301\177\265";
}
....
....

出力:

 HW Address  |start time  | end time | IP 
 78:d6:f0:c1:7f:b5 | 2012/10/05 21:18:41 | 2012/10/05 22:48:15 | 192.168.98.25
 56:a3:f0:d1:75:b5 | 2012/10/04 12:23:23 | 2012/10/05 22:48:15 | 192.168.10.1

これは私がこれまでに試したことです:

$readfile = file('/etc/dhcp/dhcpd.leases',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$arstring = implode(" ",$readfile);

$regex=array();
preg_match_all("(lease\s*([0-9.]+)\s*{\s*starts\s*.?\s*([0-9/\s:]*).?\s*ends\s*.?\s*([0-9/\s:]*).*ethernet\s*([\w:]*))", $arstring, $regex,PREG_SET_ORDER);

それは私に与えます:

[0] => Array
    (
        [0] => lease 192.168.98.25 {   starts 5 2012/10/05 21:18:41;   ends 5 2012/10/05 22:18:41;   tstp 5 2012/10/05 22:18:41;   binding state free;   hardware ethernet 78:d6:f0:c1:7f:b5;   uid "\001x\326\360\301\177\265"; } lease 192.168.98.25 {   starts 5 2012/10/05 22:54:15;   ends 5 2012/10/05 23:54:15;   binding state active;   next binding state free;   hardware ethernet 78:d6:f0:c1:7f:b5;   uid "\001x\326\360\301\177\265"; } lease 192.168.98.25 {   starts 5 2012/10/05 22:54:16;   ends 5 2012/10/05 23:54:16;   binding state active;   next binding state free;   hardware ethernet 78:d6:f0:c1:7f:b5
        [1] => 192.168.98.25
        [2] => 2012/10/05 21:18:41
        [3] => 2012/10/05 22:18:41
        [4] => 78:d6:f0:c1:7f:b5
    )
4

2 に答える 2

0

PHPがよくわからないので、正確なコードを提供することはできませんが、これを置換に変換してループで実行すると、一致する部分がなくなるまで、一致した部分を空白に置き換えます。トリックを行う可能性があります。

于 2013-03-08T04:10:36.557 に答える
0

私が最終的に理解した問題は、. をエスケープしていないことでした。および / 修飾子。

これは私の作業正規表現です:

$reg_str = '/(lease\s*([0-9\.]+)\s*{\s*starts\s*.?\s*([0-9\/\s:]*).?\s*ends\s*.?\s*([0-9\/:\s\w]*);[\r\n\s\w;]*ethernet\s*([\w:]*);?[\w\s\\\"0-9\s\r]*;?\s*[\w\-]*\s*"?([\w-]*)"?;?\s*})/ims';
preg_match_all($reg_str, $arstring, $leases, PREG_SET_ORDER);

ご協力ありがとうございました。

于 2013-03-11T16:31:36.397 に答える