Location: PHPKode > scripts > Check your Google search rankings via PHP > check-your-google-search-rankings-via-php/misc_12.exec.php
// $searchquery is the value to search for.
// The script replaces the spaces and ampersands and
// converts them to values that google is expecting.

// $searchurl is the url to find - ie www.web-max.ca
// Do not pass http:// - you don't need it.

if(!empty($searchquery) && !empty($searchurl))
	$query = str_replace(" ","+",$searchquery);	
	$query = str_replace("%26","&",$query);	

// How many results to search through.

	$total_to_search = 100;

// The number of hits per page.	

	$hits_per_page   = 10;
// Obviously, the total pages / queries we will be doing is
// $total_to_search / $hits_per_page
// This will be our rank
	$position      = 0;

// This is the rank minus the duplicates
	$real_position = 0;

	$found   = NULL;
	$lastURL = NULL;

	for($i=0;$i<$total_to_search && empty($found);$i+=$hits_per_page)

// Open the search page.
// We are filling in certain variables - 
// $query,$hits_per_page and $start.

		$filename = "http://www.google.com/search?as_q=$query".

		$file = fopen($filename, "r");
		if (!$file) 
			echo "<p>Unable to open remote file $filename.\n";

// Now load the file into a variable line at a time

			while (!feof($file))
				$var = fgets($file, 1024);

// Try and find the font tag google uses to show the site URL

				if(eregi("<font color=#008000>(.*)</font><nobr>",$var,$out))

// If we find it take out any <B> </B> tags - google does
// highlight search terms within URLS

					$out[1] = strtolower(strip_tags($out[1]));

// Get the domain name by looking for the first /

					$x = strpos($out[1],"/");

// and get the URL

					$url = substr($out[1],0,$x);


// If you want to see the hits, set $trace to something


// If the last result process is the same as this one, it
// is a nest or internal domain result, so don't count it
// on $real_position


					$lastURL = $url;

// Else if the sites match we have found it!!!

						$found = $position;
// We quit out, we don't need to go any further.

		$result = "The site $searchurl is at position $found ".
			  "( $real_position ) for the term <b>$searchquery</b>";
		$result = "The site $searchurl is not in the top $total_to_search ".
			  "for the term <b>$searchquery</b>";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../webmax.css" rel="stylesheet" type="text/css">

<body class="bodytext">
<table width="450" border="0" align="center" cellpadding="0" cellspacing="0" class="bodytext">
<td width="144"><p>Enter search term:</p></td>
<td width="255"><input name="searchquery" type="text" class="bodytext" id="searchquery2" value="<?php print($searchquery);?>"></td>
<td>Enter URL to search for:</td>
<td><input name="searchurl" type="text" class="bodytext" id="searchurl2" value="<?php print($searchurl);?>" size="40"></td>
<td><input name="Submit" type="submit" class="bodytext" value="Search"></td>
<td colspan="2"><?php print($result);?></td>
Return current item: Check your Google search rankings via PHP