为了账号安全,请及时绑定邮箱和手机立即绑定

使用 RegExp 从 Whois 中提取信息

使用 RegExp 从 Whois 中提取信息

PHP
九州编程 2022-11-12 13:19:32
如何从 Whois 查询结果中提取多个分段?我得到一个形成 Whois 查找结果的数组(来自 foreach 循环)。例如,如果我想要从 WHOIS 数据库的“域....”行到“>>> 上次更新”的所有内容:-line。我怎么做?Whois 是使用 exec 命令执行的:foreach ($query as $domain) {                           $scanUrl = 'whois '.$domain->url;            exec($scanUrl, $output);                 }Whois 可以正常工作,我可以使用 preg_grep 获取创建的、过期的和注册商:    $domainCreated  = preg_grep('/created/', $output);    $domainExpires  = preg_grep('/expires/', $output);    $domainRegistrar  = preg_grep('/registrar..........:/', $output);但是我需要得到的是数组中的多个部分,例如从域...行到 >>> WHOIS 数据库的最后更新:-行。
查看完整描述

1 回答

?
FFIVE

TA贡献1797条经验 获得超6个赞

一种处理方法是获取命令$output返回的数组并将exec其转换回单个字符串:


$text = implode("\n", $output)

然后使用preg_match_all获取所有关键字和值


preg_match_all('/^(.*?)\\.*: (.+)/m', $text, $matches);

然后$matches[1][n]将具有关键字n并$matches[2][n]具有值n。


正则表达式演示


^             # Start of line in multiline mode

(             # Start of capture group 1

   .*?        # Match 0 or more characters until ...

)             # End of capture group 1

\.*           # Match 0 or more periods

:             # Match a colon followed by a space

(             # Start of capture group 2

   .+         # Match 1 or more characters up to but not including a newline

)             # End of capture group 2

更新


每次通过循环,您将处理一个域和关键字/值对。你将如何处理这些取决于你。


foreach ($query as $domain) {

    $scanUrl = 'whois '. $domain->url;

    $output = []; // start with an empty array

    exec($scanUrl, $output);

    $text = implode("\n", $output);

    preg_match_all('/^(.*?)\\.*: (.+)/m', $text, $matches);

    $n = count($matches[1]); // number of keyword/value pairs

    for ($i = 0; $i < $n; $i++) {

        // display next keyword/value pair:

        echo $matches[1][$i], "->", $matches[2][$i], "\n";

    }

}

更新 2


与其将exec命令返回的行数组合并为单个字符串并做,这将为您提供一个匹配数组,不如对命令中的各个输出行preg_match_all进行单独调用可能更方便:preg_matchexec


foreach ($query as $domain) {

    $scanUrl = 'whois '. $domain->url;

    $output = []; // start with an empty array

    exec($scanUrl, $output);

    foreach ($output as $line) {

         if (preg_match('/^(.*?)\\.*: (.+)/', $line, $matches)) {

             echo $matches[1], "->", $matches[2], "\n";

         }

    }    

}


查看完整回答
反对 回复 2022-11-12
  • 1 回答
  • 0 关注
  • 84 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信