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.
Asked
Active
Viewed 2.2k times
4 Answers
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,529
- 11
- 41
- 45
-
2This 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,567
- 18
- 58
- 85

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
}

Alana Storm
- 164,128
- 91
- 395
- 599
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
- 55,315
- 8
- 84
- 99