problem with 2 word form entry

Hi,

 I have the attached script which works well if I enter an single word (and a url) but if I enter 2 words (eg. "pizza delivery") separated by a space (and the url) I get an error message.

If I enter the 2 words with a "+" between them (eg "pizza+delivery") the script works perfectly.

Can someone take a look at this and tell me what I need to do to to allow the user to enter two words separated by a space without errors.

Here is a running example of the script with a single word "pizza". you can enter two words separated by a space to see the resulting error.

http://www.prontopage.net/a_parse/complist.php?keyword=pizza&url=http://www.littlecaesars.com&Submit=Submit



Thanks,
<?php
error_reporting(E_ALL ^ E_NOTICE);
set_time_limit(0);
//global variable
$alexa_backlink=0;
$alexa_reach=0;
$techno_inblogs=0;
$techno_inlinks=0;
$techno_update='';

//--> for google pagerank
function StrToNum($Str, $Check, $Magic)
{
	$Int32Unit = 4294967296;  // 2^32

	$length = strlen($Str);
	for ($i = 0; $i < $length; $i++)
	{
		$Check *= $Magic;
		//If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31),
		//  the result of converting to integer is undefined
		if ($Check >= $Int32Unit)
		{
			$Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
			//if the check less than -2^31
			$Check = ($Check < -2147483648) ? ($Check + $Int32Unit) : $Check;
		}
		$Check += ord($Str{$i});
	}
	return $Check;
}

//--> for google pagerank
/*
* Genearate a hash for a url
*/
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);
}

//--> for google pagerank
/*
* genearate a checksum for the hash string
*/
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;
}

//get google pagerank
function getpagerank($url)
{
    $query="http://toolbarqueries.google.com/search?client=navclient-auto&ch=".CheckHash(HashURL($url)). "&features=Rank&q=info:".$url."&num=100&filter=0";
    $data=file_get_contents_curl($query);
    //print_r($data);
    $pos = strpos($data, "Rank_");
    if($pos === false){} else
    {
        $pagerank = substr($data, $pos + 9);
        return $pagerank;
    }
}




//for POST request with curl
function do_post_request_curl($url, $data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
    curl_setopt($ch, CURLOPT_POST, 1); // set POST method
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // add POST fields
    $result = curl_exec($ch); // run the whole process
    //echo $result;
    curl_close($ch);
    return $result;
}

function file_get_contents_curl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
    curl_setopt($ch, CURLOPT_URL, $url);
    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}
function getUrl ($sel_keyword) {
	$arrLinks=array();
	$cnt=0;

	$url = 'http://www.google.com/search?hl=en&q='.$sel_keyword.'&btnG=Google+Search&aq=f&oq=';
	$str = "'".file_get_contents($url)."'";

    $h1count = preg_match_all('/\<li class=g\>\<h3 class=r\>\<a href="(.*?)" class=l\>/',$str,$patterns);

    $href_add = $patterns[1];


    if(!empty($href_add[0]))
    {
        foreach($href_add as $key => $val)
        {
        $val = preg_replace("/</","&lt;a",$val);
            //echo "<li>" . htmlentities($val) . "</li>";
            $arrLinks[$cnt]=htmlentities($val);
            ++$cnt;
            if($cnt==3)
            {
				break;
            }

        }
        echo "</ul>";
    }
	return $arrLinks;
}
?>







<body>
<form id="form1" name="form1" method="get" action="">
  KEYWORD:
	<input name="keyword" type="text" id="keyword" value="<?php echo $_REQUEST['keyword']; ?>" />
  URL :
  	<input name="url" type="text" size="30" id="url" value="<?php echo $_REQUEST['url']; ?>" />
  	<input type="submit" name="Submit" value="Submit" />
  	<br><i><b>NOTE: The analyser sometimes may take 2-3 minutes to load the results.</b></i><br><br>
</form>
<?php
	if ((isset($_REQUEST['Submit'])) && ($_REQUEST['url']!='')) {
?>
<fieldset>
<legend>Query result</legend>
<?php
	$urlarray = array();

	$urlarray[0]=$_REQUEST['url'];


	$arrLinks=getUrl($_REQUEST['keyword']);

	for($i=0,$j=1;$i<count($arrLinks);$i++,$j++){
		$urlarray[$j]=$arrLinks[$i];
	}


?>

<table border=1 width="80%">
	<tr>
		<th>Link</th>
		<th>Google pagerank</th>
	</tr>
<?php for($i=0;$i<count($urlarray);$i++){?>
	<tr>
		<td>
			<?php
			if($i==0){
			?>
				<a href="<?php echo $urlarray[$i];?>" style="color:blue"><?php echo $urlarray[$i];?></a>
			<?php
			}
			else{
			?>
				<a href="<?php echo $urlarray[$i];?>"><?php echo $urlarray[$i];?></a>
			<?php
			}
			?>		</td>
		<td>
			<?php echo getpagerank($urlarray[$i]);?>		</td>
	</tr>
<?php } ?>
</table>

</fieldset>
<?php
}
else if(isset($_REQUEST['Submit']))
{
	echo 'Please Enter a URL.';
}
?>
</body>
</html>

Open in new window

chrisj1963Asked:
Who is Participating?
 
VanHackmanConnect With a Mentor Commented:
The code fixed:
<?php
error_reporting(E_ALL ^ E_NOTICE);
set_time_limit(0);
//global variable
$alexa_backlink=0;
$alexa_reach=0;
$techno_inblogs=0;
$techno_inlinks=0;
$techno_update='';

//--> for google pagerank
function StrToNum($Str, $Check, $Magic)
{
        $Int32Unit = 4294967296;  // 2^32

        $length = strlen($Str);
        for ($i = 0; $i < $length; $i++)
        {
                $Check *= $Magic;
                //If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31),
                //  the result of converting to integer is undefined
                if ($Check >= $Int32Unit)
                {
                        $Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
                        //if the check less than -2^31
                        $Check = ($Check < -2147483648) ? ($Check + $Int32Unit) : $Check;
                }
                $Check += ord($Str{$i});
        }
        return $Check;
}

//--> for google pagerank
/*
* Genearate a hash for a url
*/
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);
}

//--> for google pagerank
/*
* genearate a checksum for the hash string
*/
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;
}

//get google pagerank
function getpagerank($url)
{
    $query="http://toolbarqueries.google.com/search?client=navclient-auto&ch=".CheckHash(HashURL($url)). "&features=Rank&q=info:".$url."&num=100&filter=0";
    $data=file_get_contents_curl($query);
    //print_r($data);
    $pos = strpos($data, "Rank_");
    if($pos === false){} else
    {
        $pagerank = substr($data, $pos + 9);
        return $pagerank;
    }
}




//for POST request with curl
function do_post_request_curl($url, $data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
    curl_setopt($ch, CURLOPT_POST, 1); // set POST method
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // add POST fields
    $result = curl_exec($ch); // run the whole process
    //echo $result;
    curl_close($ch);
    return $result;
}

function file_get_contents_curl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
    curl_setopt($ch, CURLOPT_URL, $url);
    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}
function getUrl ($sel_keyword) {
        $arrLinks=array();
        $cnt=0;
		
		$sel_keyword = str_replace(' ','+',$sel_keyword);
		
        $url = 'http://www.google.com/search?hl=en&q='.$sel_keyword.'&btnG=Google+Search&aq=f&oq=';
        $str = "'".file_get_contents($url)."'";

    $h1count = preg_match_all('/\<li class=g\>\<h3 class=r\>\<a href="(.*?)" class=l\>/',$str,$patterns);

    $href_add = $patterns[1];


    if(!empty($href_add[0]))
    {
        foreach($href_add as $key => $val)
        {
        $val = preg_replace("/</","&lt;a",$val);
            //echo "<li>" . htmlentities($val) . "</li>";
            $arrLinks[$cnt]=htmlentities($val);
            ++$cnt;
            if($cnt==3)
            {
                                break;
            }

        }
        echo "</ul>";
    }
        return $arrLinks;
}
?>







<body>
<form id="form1" name="form1" method="get" action="">
  KEYWORD:
        <input name="keyword" type="text" id="keyword" value="<?php echo $_REQUEST['keyword']; ?>" />
  URL :
        <input name="url" type="text" size="30" id="url" value="<?php echo $_REQUEST['url']; ?>" />
        <input type="submit" name="Submit" value="Submit" />
        <br><i><b>NOTE: The analyser sometimes may take 2-3 minutes to load the results.</b></i><br><br>
</form>
<?php
        if ((isset($_REQUEST['Submit'])) && ($_REQUEST['url']!='')) {
?>
<fieldset>
<legend>Query result</legend>
<?php
        $urlarray = array();

        $urlarray[0]=$_REQUEST['url'];


        $arrLinks=getUrl($_REQUEST['keyword']);

        for($i=0,$j=1;$i<count($arrLinks);$i++,$j++){
                $urlarray[$j]=$arrLinks[$i];
        }


?>

<table border=1 width="80%">
        <tr>
                <th>Link</th>
                <th>Google pagerank</th>
        </tr>
<?php for($i=0;$i<count($urlarray);$i++){?>
        <tr>
                <td>
                        <?php
                        if($i==0){
                        ?>
                                <a href="<?php echo $urlarray[$i];?>" style="color:blue"><?php echo $urlarray[$i];?></a>
                        <?php
                        }
                        else{
                        ?>
                                <a href="<?php echo $urlarray[$i];?>"><?php echo $urlarray[$i];?></a>
                        <?php
                        }
                        ?>              </td>
                <td>
                        <?php echo getpagerank($urlarray[$i]);?>                </td>
        </tr>
<?php } ?>
</table>

</fieldset>
<?php
}
else if(isset($_REQUEST['Submit']))
{
        echo 'Please Enter a URL.';
}
?>
</body>
</html>

Open in new window

0
 
leakim971Connect With a Mentor PluritechnicianCommented:
Hello chrisj1963,

pizza%20delivery work
%20 for space, you need to encode you url with urlencore before use it : http://php.net/manual/fr/function.urlencode.php

Regards.

0
 
VanHackmanCommented:
Hi.

I think that a simple str_replace() will fix the problem.

Just:

$sel_keyword = str_replace(' ','+',$sel_keyword);
0
 
chrisj1963Author Commented:
Vanhackman worked right out of the box!
Thanks!
0
 
leakim971PluritechnicianCommented:
Thanks for the points!
0
All Courses

From novice to tech pro — start learning today.