Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Search for a string in remote webpage

Tags:

php

I am trying to write a script with PHP where it'll open up a text file ./urls.txt and check each domain for a specific word. I am really new to PHP.

Example: Look for the word "Hello" in the following domains. List: Domain1.LTD Domain2.LTD Domain3.LTD

and just simply print out domain name + valid/invalid.

<?PHP
$link = "http://yahoo.com"; //not sure how to loop to read each line from a file.
$linkcontents = file_get_contents($link);

$needle = "Hello";
if (strpos($linkcontents, $needle) == false) {
echo "Valid";
} else {
echo "Invalid";
}
?>
like image 556
user3610137 Avatar asked Mar 08 '26 17:03

user3610137


1 Answers

$arrayOfLinks = array(
    "http://example.com/file.txt",
    "https://www.example-site-2.com/files/file.txt"
    );
    
    $needle = "Hello";
    
    foreach($arrayOfLinks as $link){ // loop through the array
    
        $linkcontents = file_get_contents($link);
    
        if (stripos($linkcontents , $needle) !== false) { // stripos is case-insensitive search
    // the needle exists in $linkcontents
    // !== false instead of != false since stripos can return 0 meaning the needle is the first word of the contents
        echo "Valid";
        } else {
    // the word does not exist in the given text
        echo "Invalid";
    }
}
like image 82
Taku Avatar answered Mar 10 '26 06:03

Taku