2014-10-05 7 views
-3

У меня есть текстовый файл из 3500 URL-адресов. Этот текстовый файл состоит из 2500 доменов .com URLы и 1000 .es доменов URLS в следующем формате:Извлечь определенные данные из текстового файла с помощью PHP

http://www.domain1.com

http://www.domain3.es

...

Как я могу очистить полные URL-адреса .es и извлечь их в новый текстовый файл с помощью PHP?

+3

поиск сложнее: http://stackoverflow.com/questions/8526131/extract-urls-from-text- file –

+0

Это не то, что я ищу. –

ответ

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

Спасибо, человек, высоко ценим его. –

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

Спасибо за помощь –