2012-04-12 2 views
0

У меня есть сценарий, который тянет в Google Pagerank для введенных URL-адресов.Разрыв строк текстовой строки в массив

Я попытался отредактировать сценарий таким образом, что вместо того, чтобы только быть в состоянии поставить 3 URLs в вы можете вставить список не в URL.

Unfortuantly, когда я мимо некоторых URL, в нем просто помещает их рядом друг с другом и не отображает Pagerank.

Если вы хотите, чтобы проверить его и посмотреть, вы можете найти его здесь: http://php-playground.co.cc/testdir/pagerank.php

(люди сообщали вирус на URL, но его только потому, что его домен CO.CC, не нажимайте кнопку, если вы волновались :)

Вот код:

<?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> 
+0

echo "$ url"; не имеет смысла. вывод будет всегда Array() – heximal

ответ

1

фиксированный код:

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', '1'); 
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'])) 
{ 
$url = explode("\n",$_POST["url"]); 
foreach($url as $key => $url_l) 
{ 
$url_l=trim($url_l); 
    if($url_l!="http://") 
    echo "<td><b>PR:</td><td>" . fetch_google_page_rank($url_l) .  " </td><td></b>&raquo;</td><td>" . $url_l."</td></tr><br />"; 
} 
} 
?> 
</pre> 
</table> 
</body> 
</html> 
+0

Хорошо, я проверил его, и теперь я получаю это в массиве 'code'Array ([0] => www.google.com www.bing.com www.yahoo.co.uk) 'code' Я все еще не могу понять, что происходит1 – RuFFCuT

+0

И вывод этого кода: ' PR: »\t www.google.com www.bing.com www.yahoo.co.uk' – RuFFCuT

+0

@ adnan.shammount ничего себе, спасибо! к сожалению, все еще есть небольшая ошибка: ** Предупреждение: неверный аргумент, предоставленный foreach() в /home/play/public_html/testdir/pagerank.php в строке 137 ** Что это за строка ** foreach ($ _ POST ['url '] как $ key => $ url) ** :) – RuFFCuT