0

PHPを使用して、RIPE IPアドレス範囲のwhoisレコードから始まる行のコンテンツを取得したいdescr:ので、すべて次のようになります。

% This is RIPE NCC's Routing Information Service
% whois gateway to collected BGP Routing Tables
% IPv4 or IPv6 address to origin prefix match
%
% For more information visit http://www.ripe.net/ris/riswhois.html

route:        53.0.0.0/8
origin:       AS31399
descr:        DAIMLER-AS Daimler Autonomous System
lastupd-frst: 2011-12-08 23:18Z  195.66.224.97@rrc01
lastupd-last: 2012-01-25 15:18Z  203.119.76.3@rrc00
seen-at:      rrc00,rrc01,rrc03,rrc04,rrc05,rrc07,rrc10,rrc11,rrc12,rrc13,rrc14,rrc15,rrc16
num-rispeers: 98
source:       RISWHOIS

だから私はDAIMLER-AS Daimler Autonomous System結果として得る必要があります。

最小限のコードでこれを行う方法、私は$whoisにレコードを持っています。

<?php 
$whois = shell_exec('whois -h riswhois.ripe.net ' . $ip);
?>
4

3 に答える 3

5

これは、次を使用して実行できますpreg_match()

$whois = shell_exec('whois -h riswhois.ripe.net ' . $ip);
$result = preg_match('/^descr:\s*(.+)$/m', $matches);
$descr = $matches[1];

マルチリン(m)修飾子の使用に注意してください。

于 2012-01-25T19:54:28.247 に答える
3

文字列を行の配列に分割することから始めます。

$lines = explode("\n", $whois);

descr:次に、それらをループして、 :で始まるものを見つけます。

foreach($lines as $l) {
    if (strpos($l, 'descr:') === 0) { //We have a winner
        $description = trim(substr($l, strlen('descr:')));
    }
}

または、Timのような正規表現ソリューションを使用してください。

于 2012-01-25T19:56:01.727 に答える
2

Timのようなpreg_matchソリューションが最適ですが、参照用に、特に短い文字列用に3番目の提案を出すだけです。同じことが次の方法でも実現できます。

$descr_starts_at = strpos($my_text,"descr:") + 14;
$length = strpos($my_text,"lastupd-frst:") - 1 - $descr_starts_at;

$descr = substr($my_text, $descr_starts_at ,$length);
于 2012-01-25T20:03:04.723 に答える