faqts : Computers : Programming : Languages : PHP : Function Libraries : File Handling

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 7 people (43%) answered Yes
Recently 1 of 5 people (20%) answered Yes

Entry

Looking for a way to grab a page from the web. Search for string "Hits", then display result (eg 2525) if page showed "Hits: 2525"

Sep 29th, 2004 12:43
Gabriele F, Mark Aubrey,


Try:

<?php
$matches = array();
$url = 'http://some.server.somwhere.net/path/file.php';
$array_of_lines = file($url);
if($array_of_lines) {
  $html = implode('', $array_of_lines);
  if( preg_match( '/Hits: (\d+)/',$html ) ) {
    echo "Found $matches[1] Hits!\n";
  } else {
    echo "Uhhh.... did not find any matches.\n";
  }
} else {
  echo "I tried and tried but could not read '$url'.\n";
}
?>

Or for PHP 4.3.0 or later, a slightly cleaner version:

<?php
$matches = array();
$url = 'http://some.server.somwhere.net/path/file.php';
$html = file_get_contents($url);
if($html) {
  if( preg_match( '/Hits: (\d+)/',$html ) ) {
    echo "Found $matches[1] Hits!\n";
  } else {
    echo "Uhhh.... did not find any matches.\n";
  }
} else {
  echo "I tried and tried but could not read '$url'.\n";
}
?>

NOTE: "allow_url_fopen" must be set to "on" in your "php.ini" 
or "httpd.conf" file.
SEE ALSO:
http://www.php.net/manual/en/function.preg-match.php
http://www.php.net/manual/en/function.file.php
http://www.php.net/manual/en/function.file-get-contents.php
http://www.php.net/manual/en/function.ini-set.php