1 回答

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";
}
}
}
- 1 回答
- 0 關注
- 124 瀏覽
添加回答
舉報