1

In php I need to get the contents of a url (source) search for a string "maybe baby love you" and if it does not contain this then do x.

Mat
  • 195,986
  • 40
  • 382
  • 396
zuk1
  • 17,211
  • 21
  • 57
  • 63

4 Answers4

6

Just read the contents of the page as you would read a file. PHP does the connection stuff for you. Then just look for the string via regex or simple string comparison.

$url = 'http://my.url.com/';
$data = file_get_contents( $url );

if ( strpos( 'maybe baby love you', $data ) === false )
{

    // do something

}
okoman
  • 5,409
  • 10
  • 39
  • 45
  • 2
    This should be "strpos($data, 'maybe baby love you');" [strpos](http://php.net/manual/en/function.strpos.php) – moteutsch May 24 '11 at 21:41
5
//The Answer No 3 Is good But a small Mistake in the function strpos() I have correction the code bellow.

$url = 'http://my.url.com/';
$data = file_get_contents( $url );

if ( strpos($data,'maybe baby love you' ) === false )
{
    // do something
}
user
  • 6,166
  • 18
  • 55
  • 83
Tarek Ahmed
  • 51
  • 1
  • 1
0

Assuming fopen URL Wrappers are on ...

$string = file_get_contents('http://example.com/file.html');
if(strpos ('maybe baby love you', $string) === false){  
    //do X
}
Alan Storm
  • 161,083
  • 88
  • 380
  • 577
0

If fopen URL wrappers are not enabled, you may be able to use the curl module (see http://www.php.net/curl )

Curl also gives you the ability to deal with authenticated pages, redirects, etc.

Hugh Bothwell
  • 53,141
  • 7
  • 81
  • 98