
Clicking the "Cached" link above takes you to this cached copy of the ILoveJackDaniels.com front page.
You will see, on that page, that the keyword I used to find the page ("ilovejackdaniels") is highlighted in the cached copy of the page. This is one of Google's best features. When searching, many people frequently click through to Google's cache rather than visit the original page, simply because it is easier to pick out information when what you are looking for is highlighted for you.
In order to take this a step further, it would be great to enable this kind of functionality on a site directly. For that reason, I have written the tutorial below. It's a simple technique to allow you to serve a page to users visiting from Google, MSN, AllTheWeb, Yahoo or LookSmart, with the keywords or keywords they used to find the page highlighted for them.
Most developers are quite capable of writing a highlighting tool for their own site. However, most developers do not have the time to write something along these lines. Sometimes, there are just too many files to edit. For that reason, the solution below provides highlighting for keywords without requiring any editing to the current PHP scripts behind a site.
To begin with, we need to create (or edit) an htaccess file with the following:
php_value auto_prepend_file /full/path/to/start_google_highlight.php
php_value auto_append_file /full/path/to/end_google_highlight.phpThe two lines above operate like includes, the first adding a file to the beginning of each php script run on the server and the second adding a file to the end, both operating the same way as as the include() function in PHP.
The first of these files, below, tells the PHP parser to buffer the output from PHP. That means that it will not send anything to the browser until we tell it to. The idea is that, before anything is sent out, we can grab the page and perform some magic on it. At that stage, the page will be prepared and ready to be viewed - we just need to add our highlighting. The code below should be copied and pasted to a file called "start_google_highlight.php".
Note: Please be aware that if your site is already using output buffering, these scripts may not have the desired effect. In addition, output buffering is not available in versions of PHP previous to 4.
<?php
ob_start();
?>The second file, below, is included at the end of each PHP file (and this code should be copied and pasted to a file called "end_google_highlight.php").
<?php
// 10 colours for highlighting
$colours[0] = '#FFFF99';
$colours[1] = '#99FFFF';
$colours[2] = '#99FF99';
$colours[3] = '#FF9999';
$colours[4] = '#FF99FF';
$colours[5] = '#9999FF';
$colours[6] = '#999999';
$colours[7] = '#886800';
$colours[8] = '#004699';
$colours[9] = '#990099';
if ((isset($_SERVER['HTTP_REFERER'])) and ($_SERVER['HTTP_REFERER'] != '')) {
$keywords = "";
$url = urldecode($_SERVER['HTTP_REFERER']);
// Google
if (eregi("www\.google",$url)) {
preg_match("'(\?|&)q=(.*?)(&|$)'si", " $url ", $keywords);
$search_engine = 'Google';
}
// AllTheWeb
if (eregi("www\.alltheweb",$url)) {
preg_match("'(\?|&)q=(.*?)(&|$)'si", " $url ", $keywords);
$search_engine = 'AllTheWeb';
}
// MSN
if (eregi("search\.msn",$url)) {
preg_match("'(\?|&)q=(.*?)(&|$)'si", " $url ", $keywords);
$search_engine = 'MSN';
}
// Yahoo
if ((eregi("yahoo\.com",$url)) or (eregi("search\.yahoo",$url))) {
preg_match("'(\?|&)p=(.*?)(&|$)'si", " $url ", $keywords);
$search_engine = 'Yahoo';
}
// Looksmart
if (eregi("looksmart\.com",$url)) {
preg_match("'(\?|&)qt=(.*?)(&|$)'si", " $url ", $keywords);
$search_engine = 'Looksmart';
}
if (($keywords[2] != '') and ($keywords[2] != ' ')) {
$keywords = preg_replace('/"|\'/', '', $keywords[2]); // Remove quotes
$keyword_array = preg_split("/[\s,\+\.]+/",$keywords); // Create keyword array
}
$j = (sizeof($keyword_array) > 10) ? 10 : sizeof($keyword_array);
if ($j > 0) {
$page_contents = ob_get_contents();
ob_end_clean();
$page_parts = explode('<body', $page_contents);
$page_body = '<body' . $page_parts[1];
$keywords_list = '';
for ($i = 0; $i < $j; $i++) {
//$page_contents = preg_replace('/(>)([^<]*)([^a-z]+)(' . $keyword_array[$i] . ')([^a-z]+)/i', '$1$2$3<span style="font-weight: bold; background-color: ' . $colours[$i] . ';">$4</span>$5', $page_contents);
$page_body = str_replace('\"', '"', substr(preg_replace('#(\>(((?>([^><]+|(?R)))*)\<))#se', "preg_replace('#\b(" . $keyword_array[$i] . ")\b#i', '<span style=\"font-weight: bold; background-color: " . $colours[$i] . ";\"><b>\\\\1</b></span>', '\\0')", '>' . $page_body . '<'), 1, -1));
$keywords_list .= $keyword_array[$i] . ', ';
}
$notice = '<div style="border-bottom: 1px solid #000; font-size: 80%; padding: 3px;">Welcome, ' . $search_engine . ' user. The following search terms have been highlighted: ' . substr($keywords_list, 0, -2) . '<br><a href="http://' . $HTTP_HOST . $REQUEST_URI . '">Click here to remove highlighting</a></div>';
$page_body = eregi_replace("(<body[^>]*>)", "\\1" . $notice, $page_body);
echo $page_parts[0] . $page_body;
}
}
?>It starts out simply enough. The first part of the script allows you to define the 10 colours you want to use to highlight keywords. The first keyword the user entered will be highlighted with the first colour in the list, and so on.
Next, the script checks to see if the user's browser sent us the address that referred them to this page, in the "HTTP_REFERER" header. If so, then we check to see if they were referred from one of the 5 search engines we are checking for. If they are, then the next stage is to strip the keywords they searched for out of the referral URL.
Once we have that, we process the keywords, and turn them into an array. Then, one item at a time, we wrap a <span> around each keyword with the highlight colour in it. The script is careful not to replace anything in the <head> portion of the page, and not to replace keywords that may appear inside HTML tags.
Last, we add a note to the top of the page explaining what is going on, and add a quick link so that the user can remove the highlighting if they like.
That is literally it. You can, if you like, expand the script quite easily to include other search engines. You can change the method of highlighting, use external CSS, underline text or use a different font colour - whatever suits you. And best of all, no editing of your site is required.

ILoveJackDaniels.com is the online playground of