0

使用 PHP,我想descr:从 RIPE IP 地址范围的 whois 记录中获取该行的内容,所以它们应该看起来像这样:

% 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

You can accomplish this using preg_match():

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

Note the use of the mutliline (m) modifier.

于 2012-01-25T19:54:28.247 回答
3

I would start by splitting the string into an array of lines:

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

Then loop through them and find the one that starts with descr::

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

Or, you know, use a Regex solution like Tim's.

于 2012-01-25T19:56:01.727 回答
2

像 Tim's 这样的 preg_match 解决方案将是最佳的,但只是提出第三个建议以供参考,特别是对于较短的字符串。同样的事情可以通过以下方式完成:

$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 回答