Welcome to the OzoneAsylum FaqWiki
Frequently Asked Questions
Webdesign

How do I highlight terms on my page like Google? Pages that link to <a href="https://ozoneasylum.com/backlink?for=5672" title="Pages that link to How do I highlight terms on my page like Google?" rel="nofollow" >How do I highlight terms on my page like Google?\

There are client-side and server-side techniques for this (see the following resources). Pugzly has thrown in a quick solution:

quote:
Here's how I do it in PHP:

code:
if (isset($highlight)){
$highlight = urldecode($highlight);
$body = preg_replace("'$highlight'i","<span class="highlight">\0</span>",$body);
}

]
$highlight is grabbed from the URL, urldecoded (strips the "+"), then uses a css class called highlight to actually do the highlighting. An example can be seen at http://www.macombsheriff.com/articles/537/highlight/Pat+Richard




A quick explanation:

1. urldecode will change something like:

Pat+Richard

into:

Pat Richard

2. The regular expresion will replace incidences of the word or phrase with the word or phrase wrapped in span tags which will allow you to colour the text. So:

Pat Richard

becomes:

<span class="highlight">Pat Richard</span>

3. This is a simple example to get you going and you can expand it by separating out the words (using explode() for example) and running them all through the highlighting script.

--------------------------
Relevant threads:

Look what I did! - Slime's announcement of his JavaScript solution.

How does Google highlight the search terms? - includes Pugzly's simple PHP solution.

--------------------------
Relevant links:

Google Hilite - a script to capture Google's text highlighting commands and use your own.

___________________
Emperor

(Added by: Emperor on Sun 11-May-2003)

(Edited by: Emperor on Mon 28-Jul-2003)

« BackwardsOnwards »

Show Forum Drop Down Menu