john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

grank

<html>
<head><title>Google Search Engine Ranking Tool by John Pfeiffer</title>
<!--style type="text/css">

body{
    font-family: Verdana;
    font-size: 12pt;

    }

</style-->
</head>
<body>
Trying to find what rank a website has for certain keywords in google (out of the top 100 results, case insensitive).

<br />&nbsp;<br />

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  Multiple keyword searches, spaces between keywords, one set of keywords per line:<br />
  <textarea name="multiquery" wrap="hard" rows="5" cols="80"></textarea>
  <br />&nbsp;<br />

  target url:&nbsp;<input type="text" name="target_url"  maxlength="256" />
 <br />&nbsp;<br />
  <input type="Submit" value="Submit" />
</form>

<?php       //updated 2009-08-16-john-pfeiffer

// *********************************************************************
// ensure user has filled in query and target_url
if( !isset($_POST['multiquery']) || empty($_POST['multiquery'] ) ||
    !isset($_POST['target_url']) || empty($_POST['target_url'] )    )
{
    echo "<b>You must fill in both the query and target url.</b>";
}
else
{   $target_url = strtolower($_POST['target_url']); //url is case insensitive, displayed all lower

    $multiquery=nl2br($_POST['multiquery']);        //convert line breaks to <br />
    $multiquery_array = explode("<br />", $multiquery);     //DEBUG: print_r($multiquery_array);

    echo '<table border="1"><tr>
            <td>google query</td><td>keywords</td><td>ranking(s)</td>
                 </tr>';

    echo "<br />\n";

    for( $i=0; $i< sizeof($multiquery_array); $i++)
    {
        $query = trim($multiquery_array[$i]);       //ensure no leading/trailing whitespace!
        $query = strtr($query, " ", "+"); //convert user's query spaces to +'s

        echo "<tr><td>";
        $result = send_query_get_contents_string($query, $target_url);
        echo "</td><td>";

        echo $query;
        echo "</td><td>";

        $ranking_array = process_string_for_ranking($result, $target_url);
        if ( empty($ranking_array) )
        {   echo "NO RESULT";   }

        echo "</td></tr>\n";
    }

    echo "</table>";
    echo "<br />&nbsp;<br />";


} //end if-else query and target url filled in


// ### FUNCTION DEFINITIONS #########################################################
function send_query_get_contents_string($query)
{
    $query = "http://google.com/search?q=" . $query . "&num=100";       //get top 100 results

    echo '<a href="' . $query . '">' . $query . '</a>';
    $result = file_get_contents($query);         //downloading the resulting page from google

    if( $result == false )
    {   exit("error getting contents from google"); }

    return $result;
} //end func send_query_get_contents_string

function process_string_for_ranking($result, $target_url)
{
    //echo "<br />\nDEBUGGING: ". $result . "<br />\n";
    $ranking_array;

    $result_array = explode("<h3 class=r>", $result);   //break string into results

    $subarray = explode("</cite>", $result_array[100]);  //cut off data past result 100
    $result_array[100] = $subarray[0];                   //replace with ONLY result 10 data

    unset($result_array[0]);                        //remove google adwords results (arr[0])
    $result_array = array_values($result_array);    //re-index and shift items down

    for( $i=0; $i< sizeof($result_array); $i++)
    {
        $pos = strpos( $result_array[$i], $target_url);

        if( $pos === false )                //strpos requires funny comparison
        {}
        else
        {   echo " " . ($i+1);              //DEBUGGING echo $result_array[$i];
            $ranking_array[] = $i+1;
        }
    }

    return $ranking_array;
} //end func process_string_for_ranking

?>
<?php //include date modified
include( $_SERVER["DOCUMENT_ROOT"] . "/includes/foot.txt");
?>

<?php //include the google analytics javascript
    include( $_SERVER["DOCUMENT_ROOT"] . "/includes/google-code.htm");
?>


</body>
</html>

  • « grank top100
  • php example trial email »

Published

May 12, 2010

Category

php

~339 words

Tags

  • grank 3
  • php 82