Location: PHPKode > scripts > Google position for provided keywords > index.php
<?php
class GOOGLER
{
    function getBetween($s,$s1,$s2=false,$offset=0) 
    {
       
	if( $s2 === false ) { $s2 = $s1; }
	$result = array();
	$L1 = strlen($s1);
	$L2 = strlen($s2);
    
	if( $L1==0 || $L2==0 ) { return false; }
	    
       do {
	    $pos1 = strpos($s,$s1,$offset);
    
	    if( $pos1 !== false ) 
	    {
		$pos1 += $L1;
    
		$pos2 = strpos($s,$s2,$pos1);
    
		if( $pos2 !== false ) 
		{
		    $key_len = $pos2 - $pos1;
		    $this_key = substr($s,$pos1,$key_len); //trim this_key
					    
					      preg_match_all("/<a[^>]+href\s*=\s*(\"|')?([^\"'\s>]+)/i", $this_key, $links);
								     
					      if (($links[2][0]!="") and !(stristr($links[2][0],"google"))) $result[] = $links[2][0]; //= link address 			  
					       
		     $offset = $pos2 + $L2;
		} else 
		{
		    $pos1 = false;
		}
	    }
	} while($pos1 !== false );
    
	return $result;
    }
    
    
    function get_content($url)
    {
	$ch = curl_init();
    
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_HEADER, 0);
    
	ob_start();
    
	curl_exec ($ch);
	curl_close ($ch);
	$string = ob_get_contents();
    
	ob_end_clean();
       
	return $string;    
    }
    
    function googlerank($keywords,$limit)
    {
	    $keywstring = str_replace(" ", "+", $keywords);
	    $pages=ceil($limit/100);
	    $content="";
	    
	    for($n=1;$n<=$pages;$n++)
	    {	
		    if($n) $start=$n*100-100;
		    
		    $url="http://www.google.ca/search?as_q=$keywstring&hl=en&client=firefox-a&channel=s&rls=org.mozilla%3Aen-US%3Aofficial&num=100&start=$start&btnG=Google+Search&as_epq=&as_oq=&as_eq=&lr=&as_ft=i&as_filetype=&as_qdr=all&as_occt=any&as_dt=i&as_sitesearch=&as_rights=&safe=images";
		    
		    $content .= $this -> get_content ($url);
	    }	
	    $res = $this -> getBetween($content,"<h3","</h3>");
	    return $res;
    }
    
    function render_form()
	    {
		    echo '<h2>GOOGLE RANKING QUERY FORM:</h2><form id="form1" name="form1" method="post" action="">
		    <table   border="0" cellpadding="5">
		      <tr>
			    <td>Site Name </td>
			    <td>
			      <label>
				    <input name="site" type="text" id="site" value="aromawebdesign.com" size="60"/>
				    </label>
		       
			    </td>
		      </tr>
		      <tr>
			    <td>Keywords</td>
			    <td><label>
			      <input name="keywords" type="text" id="keywords" value="vancouver web design" size="60"/>
			    </label></td>
		      </tr>
		      <tr>
			    <td>Limit</td>
			    <td><label>
			      <input name="limit" type="text" id="limit" value="200" />
			    </label></td>
		      </tr>
		      <tr>
			    <td>&nbsp;</td>
			    <td>&nbsp;</td>
		      </tr>
		      <tr>
			    <td>&nbsp;</td>
			    <td><label>
			      <input type="submit" name="Submit" value="Submit" />
			    </label></td>
		      </tr>
		    </table> </form>';
	    }
	    
    function render_results()
    {
	$keywords 	= "vancouver web design";
	$limit 		= "200";
	$site 		= "aromawebdesign.com";

	if((isset($_POST['site'])) and (isset($_POST['keywords'])) and (isset($_POST['limit'])))
	{
		$keywords	= $_POST['keywords'];
		$limit		= $_POST['limit'];
		$site		= $_POST['site'];
		
	}

	$res = $this -> googlerank($keywords,$limit);
	//print_r($res);exit;
	/// OUTPUT

	echo "<b>Website</b>: ".$site." ";
	echo "<b>Keywords</b>: ".$keywords."<br><br>";


	$html	= "<br><br><br><table>";
	$html  .= "<tr><td><b>Position</b></td><td><b>URL</b></td></tr>";

	$fpos=0;

	while (list($key, $value) = each($res)) 
	{
		if(substr($value,0,4) == "http")
		{
			//-----------------------
			$html .= "<tr>";    
			$pos++;
			
				
			if (stristr($value,$site))
			{ 
				$html .= "<td><b>$pos</b></td><td><b> $value</b></td>\n";
				$htmlpos .= $pos.", ";
				
				if(!$fpos) $fpos=$pos;
			}	
			else $html .= "<td>$pos</td><td>$value</td>\n";
				
			$html .= "</tr>";
			//-----------------------
		
		}
		
	}
	$html .= "</table>";

	//save_data($site,$keywords,$fpos);

	if ($htmlpos !="") echo "<b>Found on positions</b>: ".$htmlpos;
	else echo "Website not found in the first ".$limit;
	
	echo $html;
    }
}

// START ----------------------------------------

$google = new GOOGLER;

if (isset($_POST['Submit']))
{
    $google -> render_results();

} else  
{
    $google -> render_form();
}
Return current item: Google position for provided keywords