0

I have some php code which collects the keywords used to get to my website from some javascript and stores them in to a database, this all works fine except it misses the last letter off of the end of the keyword string so for example if the keyword was example it would get exampl.

//Get keywords from reffering website
 function getReferalHost()
{
$ref = parse_url( $_GET['self'] );
$refer = $ref['host'];
return $refer;
}

function getKeywords()
{
$refer = str_replace( '%2520' , '+' , urlencode( $_SERVER['REQUEST_URI'] ) );
$refer = str_replace( '%26' , '&', $refer );
$refer = str_replace( '%3D' , '=', $refer );
$host_ref = parse_url( $_GET['self'] );
$host = $host_ref['host'];

if(stristr($host,'google.'))
{
    //do google stuff
    $match = preg_match('/&q=([a-zA-Z0-9+-]+)/',$refer, $output);
    $querystring = $output[0];
    $rep = str_replace('&q=','',$querystring);
    $querystring = substr( $rep , 0 , ( strlen( $rep ) - 1 ) );
    $keywords = explode('+',$querystring);
    return $keywords;
}
elseif(stristr($host,'yahoo.'))
{
    //do yahoo stuff
    $match = preg_match('/p=([a-zA-Z0-9+-]+)/',$refer, $output);
    $querystring = $output[0];
    $rep = str_replace('p=','',$querystring);
    $querystring = substr( $rep , 0 , ( strlen( $rep ) - 1 ) );
    $keywords = explode('+',$querystring);
    return $keywords;

}
elseif( stristr($host,'msn.') or stristr($host,'bing.') or stristr($host, 'ask.com' ) )
{
    //do msn stuff
    $match = preg_match('/q=([a-zA-Z0-9+-]+)/',$refer, $output);
    $querystring = $output[0];
    $rep = str_replace('q=','',$querystring);
    $querystring = substr( $rep , 0 , ( strlen( $rep ) - 1 ) );
    $keywords = explode('+',$querystring);
    return $keywords;
}

}

define('DELIMITER' , '  ');
                     $request_uri = getReferalHost();
                     $keys = getKeywords();
                     $query_string = "";
                     foreach( $keys as $key ){
                     $query_string .= $key.DELIMITER;
                     }

Any suggestions would be much appreciated.

Thanks

Lee

2 Answers 2

0

you have:

 $querystring = substr( $rep , 0 , ( strlen( $rep ) - 1 ) );

in all cases, have you tried to remove that line?

2
  • Ok i thought that had worked, it gave me back the last letter but what it also did was dump some of the variable in to the database table for example: q=Youth
    – user1259446
    Mar 20, 2012 at 12:30
  • if you remove the line, you have to change $keywords = explode('+',$querystring); to $keywords = explode('+',$rep); as @MrCode said Mar 21, 2012 at 9:33
0

Remove this line:

$querystring = substr( $rep , 0 , ( strlen( $rep ) - 1 ) );

Then, change this line:

$keywords = explode('+',$querystring);

To:

$keywords = explode('+',$rep);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.