Thursday, April 12, 2012

Exploding text form lines into an array

I have a script that pulls in the Google Pagerank for inputted URLs.



I have tried editing the script so that instead of only being able to put 3 urls in you can paste a list of URL's in.



Unfortuantly when I past some URL's in it just puts them next to each other and doesn't display the Pagerank.



If you want to test it and see you can find it here: http://php-playground.co.cc/testdir/pagerank.php



(people have been reporting a virus on the URL but its only because its a co.cc domain, do not click if you are worried :)



Here is the code:



<?php
//If the form was submitted
if(isset($_GET['url[]'])){
//Put every new line as a new entry in the array
$url = explode("\n",trim($_POST["url[]"]));
}

echo "$url";
?>
<?php
function fetch_google_page_rank($url) {
$url = strstr($url,"http://")? $url:"http://".$url;
$fp = fsockopen("toolbarqueries.google.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET /tbr?client=navclient- auto&ch=".CheckHash(HashURL($url))."&features=Rank&q=info:".$url."&num=100&filter=0 HTTP/1.1\r\n";
$out .= "Host: toolbarqueries.google.com\r\n";
$out .= "User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114-big; Windows XP 5.1)\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);

while (!feof($fp)) {
$data = fgets($fp, 128);
$pos = strpos($data, "Rank_");
if($pos === false){} else{
$pagerank = substr($data, $pos + 9);
}
}
fclose($fp);
return (int)$pagerank;
}
}

function StrToNum($Str, $Check, $Magic) {
$Int32Unit = 4294967296; // 2^32
$length = strlen($Str);
for ($i = 0; $i < $length; $i++) {
$Check *= $Magic;
if ($Check >= $Int32Unit) {
$Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
$Check = ($Check < -2147483648)? ($Check + $Int32Unit) : $Check;
}
$Check += ord($Str{$i});
}
return $Check;
}

function HashURL($String) {
$Check1 = StrToNum($String, 0x1505, 0x21);
$Check2 = StrToNum($String, 0, 0x1003F);
$Check1 >>= 2;
$Check1 = (($Check1 >> 4) & 0x3FFFFC0 ) | ($Check1 & 0x3F);
$Check1 = (($Check1 >> 4) & 0x3FFC00 ) | ($Check1 & 0x3FF);
$Check1 = (($Check1 >> 4) & 0x3C000 ) | ($Check1 & 0x3FFF);
$T1 = (((($Check1 & 0x3C0) << 4) | ($Check1 & 0x3C)) << 2 ) | ($Check2 & 0xF0F );
$T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0x3C00)) << 0xA) | ($Check2 & 0xF0F0000 );
return ($T1 | $T2);
}

function CheckHash($Hashnum) {
$CheckByte = 0;
$Flag = 0;
$HashStr = sprintf('%u', $Hashnum) ;
$length = strlen($HashStr);
for ($i = $length - 1; $i >= 0; $i --) {
$Re = $HashStr{$i};
if (1 === ($Flag % 2)) {
$Re += $Re;
$Re = (int)($Re / 10) + ($Re % 10);
}
$CheckByte += $Re;
$Flag ++;
}
$CheckByte %= 10;
if (0!== $CheckByte) {
$CheckByte = 10 - $CheckByte;
if (1 === ($Flag % 2) ) {
if (1 === ($CheckByte % 2)) {
$CheckByte += 9;
}
$CheckByte >>= 1;
}
}
return '7'.$CheckByte.$HashStr;
}
// Google PR Finder END



?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GET Google PR</title>

<style type="text/css">
<!--
body,td,th {
font-family: Verdana, Geneva, sans-serif;
font-size: 11px;
color: #333;
}
body {
margin-left: 20px;
margin-top: 20px;
}
pre{ font-size:15px;}
-->
</style></head>
<body>

<h2>Google PR</h2>
<p>Enter your URLs </p>
<form action="" method="POST">
<p>
<textarea name="url[]" rows="10" cols="50"></textarea>
<!--<input name="url[]" type="text" id="url[]" value="http://" size="80" /><br />
<input name="url[]" type="text" id="url[]" value="http://" size="80" /><br />
<input name="url[]" type="text" id="url[]" value="http://" size="80" /><br />-->
</p>
<p><input name="findpr" type="submit" value="Find Google PageRank" />
<br />
<br />
</p>
</form>
<table>
<pre>
<?php
if(isset($_POST['findpr']))
{
foreach($_POST['url'] as $key => $url)
{
if( $_POST['url'][$key]!="http://")
echo "<td><b>PR:</td><td>" . fetch_google_page_rank($_POST['url'][$key]) . " </td><td></b>&raquo;</td><td>" . $_POST['url'][$key]."</td></tr><br />";
}
}
?>
</pre>
</table>
</body>
</html>




No comments:

Post a Comment