2014-02-24 60 views
0

我想构建制表符分隔的数据文件。我使用正则表达式来提取所需的信息,即注册人名称,注册人电子邮件和域名。我希望以制表符分隔的格式为每个域查找代码输出结果。你能给我提示我如何能做到这一点?正则表达式和制表符分隔的数据文件

<?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 

[email protected] 

google.com 

not found! 

not found! 

not found! 

Domain Name Manager 

[email protected] 

cnn.com 

Domain Administrator 

[email protected] 

msn.com 

Domain Administrator 

[email protected] 

hotmail.com 

Domain Administrator 

[email protected] 

yahoo.com 

DNS Admin 

[email protected] 

gmail.com 

期望的输出应该像

Dns Admin [email protected] google.com 
Domain Name Manager [email protected] cnn.com 
Domain Administrator [email protected] msn.com 

... 

回答

0

代替echo数据保存到数组的每个if语句的:

$data[] = $matches[0][1]; 

别的

$data[] = 'not found!'; 

然后在循环(后最后,如果)结​​束,与标签加入数据:

$lines[] = implode("\t", $data) . "\n"; 

然后循环完成后(})保存线到文件:

file_put_contents('path/to/file.txt', $lines);