我编写了一些代码来查找两个 HTML 标记之间的特定单词,然后将名称复制到文本文件中。
它适用于小型 HTML 文件。问题是它不适用于大文件(22 MB),(在某些时候它停止运行并且没有创建文件)。
如何使我的代码更具可扩展性?
<?php
$filename = "example.txt"; // file that the code search for names
$names = fopen("names.txt", 'a+'); // new file to copy the names to it
$handle = fopen($filename, "r"); // copy text from file to variable
while(!feof($handle)){ // loop until the end of the file
$line = fgets($handle); // reading 1 line from file at a time
preg_match(';(?<=from"><span class="profile fn">)(.*)</span></div>;', $line, $matches);
// searching the name between the html tags, if found --> into $matches
$match = $matches[1]; // the name is at cell 1 in array into @match
if(!empty($match)) //if @match is not empty (some lines empty if
//preg_match didn't find a match (to avoid
//empty lines in file)
{
fwrite($names, ($match."\n")); //write the name into the file "names"
}
$data1 = file("names.txt"); //create new file without duplicate names
file_put_contents('unique.txt', implode(array_unique($data1)));
}
fclose($filename);
fclose($names);
fclose('unique.txt');
?>