2009-07-16 2 views
0

PHP 정규식으로 dhcp leases 테이블을 구문 분석해야합니다. 하지만 그게 정규 표현식에서 사용되는 다른 문자를 포함하고 있습니다. 여기 복잡한 대상 문자열이있는 PHP Regex 문제

샘플 출력을하다

lease 172.17.2.3 { 
    starts 4 2009/07/16 11:54:39; 
    ends 4 2009/07/16 12:54:39; 
    cltt 4 2009/07/16 11:54:39; 
    binding state active; 
    next binding state free; 
    hardware ethernet 00:50:56:c0:00:01; 
    uid "\001\000PV\300\000\001"; 
    client-hostname "Yasin-PC"; 
} 
lease 172.17.2.3 { 
    starts 4 2009/07/16 12:24:39; 
    ends 4 2009/07/16 13:24:39; 
    cltt 4 2009/07/16 12:24:39; 
    binding state active; 
    next binding state free; 
    hardware ethernet 00:50:56:c0:00:01; 
    uid "\001\000PV\300\000\001"; 
    client-hostname "Yasin-PC"; 
} 
lease 172.17.2.3 { 
    starts 4 2009/07/16 12:54:39; 
    ends 4 2009/07/16 13:54:39; 
    cltt 4 2009/07/16 12:54:39; 
    binding state active; 
    next binding state free; 
    hardware ethernet 00:50:56:c0:00:01; 
    uid "\001\000PV\300\000\001"; 
    client-hostname "Yasin-PC"; 
} 

문제 내가 {... & 중복이있을 것이다 임대 ..XX.XX.XX.XX 후 IP 주소를 사용하여 인덱스 배열로 전체 테이블을 할당 할 것입니다 키를하지만 값이 달라질 수 있도록 내가 그렇게 해결할 필요가있을 것입니다 ...

무엇이 나를 위해 좋은 정규식을 구축하기 위해 내 시간을 절약 조언을 해주겠습니까? posix 또는 pcre 또는 라인을 한 줄씩 읽으시겠습니까?

& 대상 임대 테이블에 대해 확신 할 수 없지만 모두 동일한 형식입니다. 어쩌면 몇 번 더 몇 줄을 기대합니다.

+0

당신이 얻을 할 일이 최종 출력 모양처럼 무엇 무엇입니까? – ghostdog74

+0

3 차원 어레이는 IP 주소 ... 으로 색인화됩니다. mac-addr., hostname, 다른 배열에는 임대의 시작 및 종료 시간이 포함됩니다. 지금은 라인별로 방법을 찾으려고합니다. 내가 끝내면 나는 답으로 추가 할 것입니다. 그래서 그것을 필요로하는 누군가는 찾아서 사용할 수 있습니다. stackoverflow의 seo 기능에 감사드립니다. 이 사이트는 간단한 함수 스크립트를 작성하는 동안 발견되었습니다. – risyasin

답변

1

나는 당신이 할 수 있다고 생각 :

<?php 
$order_fields = array('starts', 'ends', 'cltt', 'binding state', 'next binding state', 'hardware ethernet', 'uid', 'client-hostname'); 
$fields_regexp = ''; 
foreach ($order_fields as $field) 
{ 
    $fields_regexp .= "\s*".$field." (.*)"; 
} 
$regexp = '/lease (\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b) \{'.$fields_regexp.'\s*\}/m'; 
preg_match_all($regexp, $string, $result, PREG_PATTERN_ORDER); 

$arr = array(); 
foreach ($result[1] as $i => $match) { 
    $cont = count($arr[$match]); 
    $arr[$match][$cont]['raw'] = $result[0][$i]; 
    $arr[$match][$cont]['ip'] = $match; 
    foreach ($order_fields as $pos => $field) 
    { 
     $arr[$match][$cont][$field] = $result[$pos + 2][$i]; 
    } 
} 
print_r($arr); 
?> 

샘플 출력 :

Array 
(
    [172.17.2.3] => Array 
     (
      [0] => Array 
       (
        [raw] => lease 172.17.2.3 { 
    starts 4 2009/07/16 11:54:39; 
    ends 4 2009/07/16 12:54:39; 
    cltt 4 2009/07/16 11:54:39; 
    binding state active; 
    next binding state free; 
    hardware ethernet 00:50:56:c0:00:01; 
    uid "�PVÀ�"; 
    client-hostname "Yasin-PC"; 
} 
        [ip] => 172.17.2.3 
        [starts] => 4 2009/07/16 11:54:39; 
        [ends] => 4 2009/07/16 12:54:39; 
        [cltt] => 4 2009/07/16 11:54:39; 
        [binding state] => active; 
        [next binding state] => free; 
        [hardware ethernet] => 00:50:56:c0:00:01; 
        [uid] => "�PVÀ�"; 
        [client-hostname] => "Yasin-PC"; 
       ) 

      [1] => Array 
       (
        [raw] => lease 172.17.2.3 { 
    starts 4 2009/07/16 12:24:39; 
    ends 4 2009/07/16 13:24:39; 
    cltt 4 2009/07/16 12:24:39; 
    binding state active; 
    next binding state free; 
    hardware ethernet 00:50:56:c0:00:01; 
    uid "�PVÀ�"; 
    client-hostname "Yasin-PC"; 
} 
        [ip] => 172.17.2.3 
        [starts] => 4 2009/07/16 12:24:39; 
        [ends] => 4 2009/07/16 13:24:39; 
        [cltt] => 4 2009/07/16 12:24:39; 
        [binding state] => active; 
        [next binding state] => free; 
        [hardware ethernet] => 00:50:56:c0:00:01; 
        [uid] => "�PVÀ�"; 
        [client-hostname] => "Yasin-PC"; 
       ) 

      [2] => Array 
       (
        [raw] => lease 172.17.2.3 { 
    starts 4 2009/07/16 12:54:39; 
    ends 4 2009/07/16 13:54:39; 
    cltt 4 2009/07/16 12:54:39; 
    binding state active; 
    next binding state free; 
    hardware ethernet 00:50:56:c0:00:01; 
    uid "�PVÀ�"; 
    client-hostname "Yasin-PC"; 
} 
        [ip] => 172.17.2.3 
        [starts] => 4 2009/07/16 12:54:39; 
        [ends] => 4 2009/07/16 13:54:39; 
        [cltt] => 4 2009/07/16 12:54:39; 
        [binding state] => active; 
        [next binding state] => free; 
        [hardware ethernet] => 00:50:56:c0:00:01; 
        [uid] => "�PVÀ�"; 
        [client-hostname] => "Yasin-PC"; 
       ) 

     ) 

) 
+0

대단히 감사합니다 ... TI는 잘 작동합니다 :) – risyasin

0

이 구문을 분석 할 수도 있습니다. 한 줄씩 읽고 변수에 현재 상태를 유지하십시오. lease ...으로 전화를 걸었을 때 lease 절이 아니라면 $inLeasetrue으로 설정하고 닫는 중괄호 }을 누르기 전까지 나머지 줄을 현재 lease에 대한 매개 변수로 처리합니다.

이 경우 정규식이 도움이 될 수 있지만 단순한 줄 단위 구문 분석은 어렵지 않습니다. 데이터 형식이 정적 인 방법을 고려할 때 정규 표현식은 과도하게 사용됩니다.

+0

당신의 방법이 전체 파일을 분석하는 것보다 낫습니다. 지금 시도 중입니다. 대답 주셔서 감사합니다 :) – risyasin