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 关注
- 84 浏览
添加回答
举报