2014-02-24 63 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);