Say I have a large file. The file is a list of IP addresses in each new line:
123.123.123.123
123.123.123.124
123.123.123.125
etc...
I could do it like this:
$file = file_get_contents($src);
if (substr_count($file,'myip'))
echo 'FOUND';
Or a similar way with array style:
$file = file($src,FILE_IGNORE_NEW_LINES);
if (in_array('myip',$file))
echo 'FOUND';
But I think there is a third option, which can be faster.
Parsing a line by line and, of course, stopping reading if a line is found.
Something like that:
$file = fopen($src,'r');
while(!feof($file)) {
$ip = fgets($file);
if ($ip == $myIP) {
die('Found');
}
}
fclose($file);
My question is: do you think there is another better way?
And in terms of performance, which code do you think is faster?
Thank you very much guys
anon
source
share