This code is to read contents of a text file that contains 100 urls one in each line. The script is to search for a particular word in the urls using file_get_contents.
<?php
$mysearch = file("phpelist.txt");
for($index = 0; $index <count($mysearch); $index++)
{ while ($index >=10 && $index <=20 ):
$mysearch[$index] = str_replace("
", "", $mysearch[$index]);
$data = file_get_contents("$mysearch[$index]");
$searchTerm = 'about';
if (stripos($data, $searchTerm) !== false) {
echo "$mysearch[$index]</strong>...FOUND WORD<br><strong>";
}
else
{
echo "$mysearch[$index]</strong>...NO SUCH WORD<br><strong>";
}
endwhile;
}
?>
Your code confused me. Looking at just the requirements statement, I got ...
$searchTerm = 'about';
$file = new SplFileObject('phpelist.txt');
foreach ($file as $n => $url) {
if ($n < 10) continue;
if ($n > 20) break;
$content = file_get_contents($url);
if (stripos($content, $searchTerm) !== false) {
echo "<strong>$url</strong>...FOUND WORD<br>";
} else {
echo "<strong>$url</strong>...NO SUCH WORD<br>";
}
}
CORRECTED: Additional requirements (see comments below). Which means its no longer much short than OP. The only thing it adds, I guess, is the use of SplFileObject
. I like using SplFileObject
because it lets you use a file like an array (without loading the entire file into memory) and so can be used in a foreach
(since SplFileObject
implements the Iterator
interface).