How do I match an exact string in a webpage in PHP?

We can easily check the match in the string   

if (preg_match("/happy/i", "happy is he who has ")) {
    echo "match found.";
} else {
    echo "match not found.";
}
?>

      

But how do you check for a match on a web page or specify a URL?

EDIT:

How do I use Regex to find if a specific string exists on a web page? I can extract all the information and display it using the code given in one of the answers.

+2


a source to share


3 answers


As Rodolphi said, using cURL is the best way to get a remote page. You don't really need to use regexp to check if the text contains a string, this will do it:



if (strpos($urlContents, $needle) !== FALSE) {
  echo "match found";
}

      

+1


a source


You can use cURL to grab a webpage and you can parse it with any regular expression! http://www.php.net/manual/en/book.curl.php



+2


a source


You can use file_get_contents

Direct from php.net

<?php
    $homepage = file_get_contents('http://www.example.com/');
    echo $homepage;
?>

      

+1


a source







All Articles