使用 RegExp 从 Whois 中提取信息


如何从 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 数据库的最后更新:-行。


九州编程
浏览 86回答 1
1回答

FFIVE

一种处理方法是获取命令$output返回的数组并将exec其转换回单个字符串:$text = implode("\n", $output)然后使用preg_match_all获取所有关键字和值preg_match_all('/^(.*?)\\.*: (.+)/m', $text, $matches);然后$matches[1][n]将具有关键字n并$matches[2][n]具有值n。正则表达式演示^&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Start of line in multiline mode(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Start of capture group 1&nbsp; &nbsp;.*?&nbsp; &nbsp; &nbsp; &nbsp; # Match 0 or more characters until ...)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# End of capture group 1\.*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Match 0 or more periods:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Match a colon followed by a space(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Start of capture group 2&nbsp; &nbsp;.+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Match 1 or more characters up to but not including a newline)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# End of capture group 2更新每次通过循环,您将处理一个域和关键字/值对。你将如何处理这些取决于你。foreach ($query as $domain) {&nbsp; &nbsp; $scanUrl = 'whois '. $domain->url;&nbsp; &nbsp; $output = []; // start with an empty array&nbsp; &nbsp; exec($scanUrl, $output);&nbsp; &nbsp; $text = implode("\n", $output);&nbsp; &nbsp; preg_match_all('/^(.*?)\\.*: (.+)/m', $text, $matches);&nbsp; &nbsp; $n = count($matches[1]); // number of keyword/value pairs&nbsp; &nbsp; for ($i = 0; $i < $n; $i++) {&nbsp; &nbsp; &nbsp; &nbsp; // display next keyword/value pair:&nbsp; &nbsp; &nbsp; &nbsp; echo $matches[1][$i], "->", $matches[2][$i], "\n";&nbsp; &nbsp; }}更新 2与其将exec命令返回的行数组合并为单个字符串并做,这将为您提供一个匹配数组,不如对命令中的各个输出行preg_match_all进行单独调用可能更方便:preg_matchexecforeach ($query as $domain) {&nbsp; &nbsp; $scanUrl = 'whois '. $domain->url;&nbsp; &nbsp; $output = []; // start with an empty array&nbsp; &nbsp; exec($scanUrl, $output);&nbsp; &nbsp; foreach ($output as $line) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (preg_match('/^(.*?)\\.*: (.+)/', $line, $matches)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo $matches[1], "->", $matches[2], "\n";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP