我想构建一个制表符分隔的数据文件。我正在使用正则表达式来提取所需的信息,即注册人姓名,注册人电子邮件和域名。我希望代码以制表符分隔的格式输出每个域查找的结果。你能给我一些关于如何实现这一点的提示吗?
<?php
require 'Net/Whois.php';
$server = 'whois.networksolutions.com';
$content=file('query-file.txt');
foreach ($content as $query)
{
$whois = new Net_Whois;
$data = $whois->query($query, $server);
if (preg_match_all('/^registrant name: (.*)/im', $data, $matches, PREG_SET_ORDER))
// Print the matches:
{
echo '<pre>' . $matches[0][1] . '</pre>';
} else {
echo 'not found!</p>';
}
if (preg_match_all('/^registrant email: (.*)/im', $data, $email, PREG_SET_ORDER))
// Print the matches:
{
echo '<pre>' . $email[0][1] . '</pre>';
} else {
echo 'not found!</p>';
}
if (preg_match_all('/^Domain Name: (.*)/im', $data, $email, PREG_SET_ORDER))
// Print the matches:
{
echo '<pre>' . $email[0][1] . '</pre>';
} else {
echo 'not found!</p>';
}
}
?> 代码的输出
Dns Admin
dns-admin@google.com
google.com
not found!
not found!
not found!
Domain Name Manager
tmgroup@turner.com
cnn.com
Domain Administrator
domains@microsoft.com
msn.com
Domain Administrator
domains@microsoft.com
hotmail.com
Domain Administrator
domainadmin@yahoo-inc.com
yahoo.com
DNS Admin
gmail-abuse@google.com
gmail.com所需的输出应如下所示
Dns Admin dns-admin@google.com google.com
Domain Name Manager tmgroup@turner.com cnn.com
Domain Administrator domains@microsoft.com msn.com
...发布于 2014-02-24 23:50:42
将每个if语句的数据保存到一个数组中,而不是echo:
$data[] = $matches[0][1];否则
$data[] = 'not found!';然后在循环的末尾(在最后一个if之后),使用制表符连接数据:
$lines[] = implode("\t", $data) . "\n";然后在循环结束后(})将行保存到一个文件中:
file_put_contents('path/to/file.txt', $lines);https://stackoverflow.com/questions/21992178
复制相似问题