使用PHP从文本文件中提取特定数据
问题描述:
我有一个3500个URL的文本文件。这个文本文件由2500页.com域名的URL和1000个.ES域名的网址的格式如下:使用PHP从文本文件中提取特定数据
...
我怎样才能刮完整的.es网址,并使用PHP将它们提取到新的文本文件?
答
这里是一个办法做到这一点
$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
答
也许这有助于
/*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
搜索更难:http://stackoverflow.com/questions/8526131/extract-urls-from-text-文件 – 2014-10-05 19:47:25
这不是我正在寻找的。 – 2014-10-05 21:18:42