2014-10-05 211 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