亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 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 關注
  • 124 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號