Highlighting Google results on webpage

The Problem
Sometimes we have a page with so much content that it's hard for everyone to find the exact content that he might be looking for. Maybe we just want to distinguish the keywords that someone used to find our website. For this we can use a simple php code.

The Code
First of all, we need to check if the visitor came from google.com, in order to do this we are going to use the $_SERVER['HTTP_REFERER'] variable:

 if(isset($_SERVER['HTTP_REFERER'])){
    if(stripos($_SERVER['HTTP_REFERER'],"google.com")!=false){ 
 
    }
 }

Then, we need to set some configuration vars:

 if(isset($_SERVER['HTTP_REFERER'])){
    if(stripos($_SERVER['HTTP_REFERER'],"google.com")!=false){
        $bgcolor = '#FF0000';
        $wordcolor = '#FFFFFF';
	$weight = 'bold';
    }
 }

$bgcolor: the word's background color.
$bgcolor: the word's color.
$weight: bold or normal.

Separate the words form the url:
...
$keywords = explode("+",substr(strstr($url,"&q="),3,
strpos(substr(strstr($url,"&q="),3),"&")));
...

What do we have there?... Let's start with the explode() command. explode() is use to create an array from a string, the first argument is the "delimiter" and the second one is the "string" itself. Why "+", the plus symbol is the separator of the keywords that google use on the URL for a search. Here is an example: "http://www.google.com/search?hl=en&q=david+tavarez&btnG=Search". Then just remove everything else but the keywords.

And the last step, highlight the words using "str_ireplace()" command:

...
 foreach($keywords as $word)
    $text = str_ireplace($word,"
      <span style="\">
 {$word}</span>",$text);
    $content =  $text; //The new content
...

And we got it!

Real world Examples
Now, lets see how we could use this code in the real world.

If you are a programmer I think you could use this code without problem, but if you are not... there are 2 examples:

Example #1 (Simulation)

Result:

Example Result

Example Result

Example #2
If you are using a dynamic content, just put the code after the output command.

Complete Script
Just click here to download the complete code Highlight Google Keywords

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
Leave a comment

3 Comments.

Leave a Reply


[ Ctrl + Enter ]

Trackbacks and Pingbacks: