2014-10-05 240 views
-3

我有一個3500個URL的文本文件。這個文本文件由2500頁.com域名的URL和1000個.ES域名的網址的格式如下:使用PHP從文本文件中提取特定數據

http://www.domain1.com

http://www.domain3.es

...

我怎樣才能刮完整的.es網址,並使用PHP將它們提取到新的文本文件?

+3

搜索更難:http://stackoverflow.com/questions/8526131/extract-urls-from-text-文件 – 2014-10-05 19:47:25

+0

這不是我正在尋找的。 – 2014-10-05 21:18:42

回答

0

這裏是一個辦法做到這一點

$source = fopen("inputfile", "r"); 
$destination = fopen("outputfile", "w"); 

if ($source && $destination) { 
    while (($line = fgets($source))) { 
     if (strpos($line,'.es')) { 

      fwrite($destination, $line); 
     } 

    } 
    fclose($source); 
    fclose($destination); 
} else { 
    // error opening the file. 
} 
+0

謝謝老兄,非常感謝。 – 2014-10-05 21:19:24

0

也許這有助於

/*read whole file into array*/ 
$linkList = file("path/to/file"); 
/*filter array*/ 
$filteredList = preg_grep("/\.es$/", $linkList); 
/*write it back to file*/ 
file_put_contents('newFile.txt', implode("\n", $filteredList)); 
+0

感謝您的幫助 – 2014-10-05 21:20:05

相關問題