I wrote some code to find a specific word between two HTML tags, and then copy the names into a text file.
It works on small HTML files. The problem is that it doesn't work on large files (22 MB), (at some point its stops running and no file is created).
How can I make my code more scalable?
<?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');
?>