views:

158

answers:

5

i want to check remote url's page contents. IF remote site's page content contains string http://yahoo.com set $qqq = YH if not contains $qqq = NOYH. i am not talking about "url of that page" im talking about page content of url

$url = "'".$get['url']."'";
$needle = "http://yahoo.com/";
$contents = file_get_contents($url);
if(stripos($contents, $needle) !== false) {
    $qqq = "YH";
}

But it's not working. Can anybody help me with the correct syntax? thanks..

+2  A: 
$url = $get['url'];
$needle = "http://yahoo.com/";
$contents = file_get_contents($url);
if(stripos($contents, $needle) !== false) {
  $qqq = "YH";
  echo $qqq; // <--in order to echo, you need to call echo.
}

If your goal is just to echo YH if it exists, you can just call it directly with,

echo "YH";

Rather than storing it into a variable.

Anthony Forloney
+1  A: 

There seems to be some confusion with your question and the title.

To answer "if $url contains http://yahoo.com/" then the following will do:

$url = "'".$get['url']."'";
$needle = "http://yahoo.com/";
if(stripos($url, $needle) !== false) {
  $qqq = "YH";
}

Of course, you can use <?=$qqq?> to output the result.

zaf
+1  A: 

It think your code won't work. For a number of reasons:

  1. In your first line, you create a string, that contains single-quotes. So basically, $url contains something like 'http://url.here'. If you pass this to file_get_contents you get an error:

    $url = "'http://www.google.com'";
    echo file_get_contents($url);
    
    
    Warning: file_get_contents('http://www.google.com/'): failed to open stream: 
    No such file or directory in ...
    
  2. You said want to check whether $url contains a certain string. But you are checking whether the document the URL is pointing to, contains this string.

3. Maybe you mean $_GET instead of $get to retrieve the parameter url that is contained in the URL?

Ok, I read from the comments that you indeed want to search for the string in the content. Still, the first line of code is wrong, so it is probably:

$needle = "http://yahoo.com/";
$contents = file_get_contents($get['url']);
if(stripos($contents, $needle) !== false) {
    $qqq = "YH";
}

(<?= $qqq ?> should work as it is).

Felix Kling
i didnt mean $_GET.. $get is mysql_fetch_arrayed mysql query. i tried your last edit. it makes my page unloadable. its never loading.
Ronnie Chester Lynwood
@Ronnie, it might makes sense to edit the whole entire your question with the entire code snippet so we can see what you are doing, besides the sole problem where we are guessing what your trying to do, it can also show if the problem lies elsewhere.
Anthony Forloney
@Ronnie Chester Lynwood: Ok, then it is not `$_GET` ;) But what is the value of `$get['url']`? If it is a string, my code should still work...
Felix Kling
A: 

You need to debug, so break it down step by step:

<?PHP
// make sure you see any errors (remove this later)
error_reporting(E_ALL);
ini_set('display_errors',1);

$url = $get['url'];
die($url);
?>

Is the URL correct?

<?PHP
// make sure you see any errors (remove this later)
error_reporting(E_ALL);
ini_set('display_errors',1);

$url = $get['url'];
die(file_get_contents($url));
?>

Does your script echo what looks like the response from $url?

Then continue building out and testing...

Without seeing all of your code, nobody here will be able to guess what you're doing wrong, but it should be easy, fun, and instructional for you to figure it out for yourself.

I hope this answer sends you off in the right direction.

timdev
A: 

Make sure you have PHP warnings on -- you should always set error_reporting(E_ALL) in a development environment anyway.

Make sure you have allowed URIs as parameters for fopen based functions allow_url_fopen - http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

Nick