使用PHP从文本文件中提取特定数据

时间:2014-10-05 19:42:34

标签: php

我有一个3,500个网址的文本文件。此文本文件由2,500个.com域名网址和1,000个.es域名网址组成,格式如下:

  

http://www.domain1.com

     

http://www.domain3.es

     

...

如何使用PHP抓取完整的.es网址并将其解压缩到新的文本文件?

2 个答案:

答案 0 :(得分: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));

答案 1 :(得分: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.
}