Wednesday, March 3, 2010

PHP display keyword position

I saved this piece of code by Matthew Inman some while ago but ever since than I always levead it for tomorrow and tomorrow and ... It does a pretty and simple job to help you on your SEO by searching for a given url along with a search term (yeah your keyword). When found it outputs the result telling you exactly where your page is located (only the first 100 results are considered) based on that keyword. There's too much talking already for this simple code..here it is: PHP
File: whererank.php
   Description:
     This script searches google for a given search term and redirects the user to the
     page of search results that contains the target URL.
   Installation:
     Rename this file have a .php extension and upload it to your web server.

*/


if (isset($_GET['url']) && isset($_GET['keyword'])) {

   $gg_url = 'http://www.google.com/search?hl=en&q=' . urlencode($_GET['keyword']) . '&start=';

   $url = preg_replace('(^http://|/$)','',$_GET['url']);

   for ($page = 0; $page < 9; $page++) {

     $handle = fopen($gg_url . $page  . 0 ,'r');

     $scraped = '';

      if ($handle) {
        while (!feof($handle)) {
            $buffer = fgets($handle, 4096);
            $scraped .= $buffer;
        }
        fclose($handle);
      }

      $results = array();
      preg_match_all('/a href="([^"]+)" class=l.+?>.+?<\/a>/',$scraped,$results);

      foreach ($results[1] as $serp) {
        $serp = preg_replace('(^http://|/$)','',$serp);
        if ($serp == $url) {
          header('Location: ' . $gg_url. $page . '0');
          exit;
        }

      }

   }
   $error_message = 'Not in top 100 search results';
}

?>

HTML:
 
 

  

  





  
Where's it rank at Google? Not found in top 100 search results Browser Button Drag this link to your browser toolbar, or right-click it and choose Bookmark This Link: Where's it rank?
You can use them in the same page.                 

No comments:

Post a Comment