php script to search multiple webpages from file for specific word

First excuse me for the bad english.

I am trying to build a php script to search multiple webpages from a .txt file for specific word.

More specific:

I have a .txt file where i have stored many urls (every url is on one line, so if i have 10 urls the file have 10 lines) and i want the script to check the webpage content of each url for a specific word. So if the word is found on the webpage the script will return ONLINE othewise will return DOWN.

I build the script but the problem is that it always return ONLINE even if the url from file doesn’t have the specific word in it’s webpage content.

[php]

<?php $allads = file("phpelist.txt"); print("Checking urls:


"); for($index = 0; $index <count($allads); $index++) { $allads[$index] = str_replace("\n", "", $allads[$index]); $data = file_get_contents("$allads[$index]"); $searchTerm = 'the'; if (stripos($data, $searchTerm) !== false) { echo "$allads[$index]...ONLINE
"; } else { echo "$allads[$index]...DOWN
"; } } print("


I verified all urls from file!"); ?>

[/php]

It seems to work fine for me. Are you sure you aren’t just using a too common word? I also get 100% hits on the search term ‘the’ on the sites I tested. Remember that you are searching through the source, not what’s displayed in a browser.

And just a small code review change, I have:

[ul][li]Split the logic and the view[/li]
[li]Wrapped the site checking in a function[/li]
[li]Removed break-tags as design should be done with css[/li]
[li]Cleaned up the loop code, not relying on indexes, using trim instead of str_replace, etc[/li]
[li]Changed status text to a status icon[/li][/ul]

[php]

<?php function checkSitesForWord($search, $sitesFile = "phpelist.txt") { $allads = file($sitesFile); $result = array(); foreach($allads as $url) { $data = file_get_contents(trim($url)); $result[$url] = (stripos($data, $search) !== false) ? 'online' : 'offline'; } return $result; } $result = checkSitesForWord('pizza'); ?> ul#sites { list-style-type: none; padding-left: 0; } ul#sites li { font-weight: bold; } ul#sites span { float: left; margin: 0 10px 0 0; width: 16px; height: 16px; } ul#sites .icon-online { background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACrUlEQVR42m2TTUhUURTH//d9zUc+G5yc/MqMpiIoqNHZuVLKVUGbCoxAB4Y2ZpG5a9XOjDI3KYxCJFSboFYKunI3NgQFUY4w5VfMoE2Ozsf7uLf73rOaGg8c3nvn3v/vnnPe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p+qr3Mar4tMjAdcLkGh0Mh5u7rg2S38wM1k0FyaQkzU1NaOJWaV4BHZM7XAixtQOR1b2+Pf2ICosWCf61SPo/o6CiGVlZWt4EbOoQDKPLcm87OwLWREQiyzKmszlmrqKwoeD85iXvxeJgyua/f63oArD0ZGGByezsExo4ArM6ZAzQNytYWni4saC+BbtYEvHonSUFnfz9EchbokvAfQLmiEkHSi4u4rqqzzAfE38qy57jPB3p9iBWAUAc4ciZRNbC/vo67ipJgt4Hic8BkdjrRJIocUAtBnbOuYqmEQjqNELHYHdq/IIAsSZDo9XkWRGDVdGB4aRSW/Hh0laqhqCoe6oAr9AsRwEOVoAzoDawCmJWSN+uGCqBIOiBIVoOWK0Mt0UOC1zHBHzFJnXeW8mXN5HyKTk7qnUGSagD7pD2C/NJVxnf6Jzd1Ji/jGJVxzAbGzh8D2sjUTFar0SB86eFypN+kbcpi6wDje9DG9TLq55Tt3IczCHg95OUgtZDxhHHAV8GIjl19MHKIJXK4+gPhQqWReCufMyGyfIn1eC87AZfdyKLa0flK9FQGsY9p3PqsrX47rGnlKsQsYPqxF4PDN1tZ68UO4HSLcfJzFztfNjCzvKM9i2G+UG4cpr/GWRIR7HbD3+EAH+eNTaTWkoiqpcZx/gO7GdtuZamYSQAAAABJRU5ErkJggg==);

Checking urls:

    <?php foreach ($result as $site => $status) {?>
  • <?= $site ?>
  • <?php } ?>
I verified all urls from file![/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service