In a view and I added an exposed filter: Search: Search Terms filter

It works fine, but I'd like to highlight the searched keyword in the results.

How can I do it?

  • You can use tpl file for this.. Commented Dec 3, 2012 at 9:42
  • yes, I guess that I should use a function to replace the searched keyword. But how do I fetch the searched keyword, and what if there are more than one keywords?
    – chefnelone
    Commented Dec 3, 2012 at 13:24
  • If you can use jQuery there is a small tricky way to do it.
    – niksmac
    Commented Dec 3, 2012 at 16:52
  • yeah think about it how to get.. you can use jquery as nikhil said.. :D Commented Dec 4, 2012 at 4:04

5 Answers 5


You could use something like highlight that finds and adds a class to all instances of a word on the page, and instead of feeding it a static word, you would use the contents of the search box.

You can find more information and links to other versions in the StackOverflow question, Highlight a word with jQuery.

  • I get it working. Is javascript but it ok. Thanks.
    – chefnelone
    Commented Dec 5, 2012 at 15:46

Using search_excerpt function you can highlight the searched keyword. It show the keyword in bold.

Returns snippets from a piece of text, with certain keywords highlighted. Used for formatting search results.

An example is shown below,

for eg: //a body field is exposed here

in your views tpl file add the following code

 <?php foreach ($rows as $id => $row): 

      $keys = $_GET['body_value'];
      $row = search_excerpt($keys, $row);

it will highlight the searched word, see the screenshot below

enter image description here

  • It does the job but it remove the structure of the view row. The view is showing content (not fields) as teaser. Once the search is done the result doesn't show the teaser but just the raw data. Can this be solved.
    – chefnelone
    Commented Dec 5, 2012 at 13:56
  • I see that the search_excerpt function remove all markup. Can this be change in some way?
    – chefnelone
    Commented Dec 5, 2012 at 14:06
  • I think this is the best option if the views is styled to show fields, but is not useful if the view is showing content (full, teaser,..)
    – chefnelone
    Commented Dec 6, 2012 at 10:53

If you are using views with Apache Solr with http://drupal.org/project/search_api and http://drupal.org/project/search_api_solr

You can enable highlighting on the word.

function custom_views_pre_render(&$view) {
  if ($view->name == 'article') {
    $result = $view->result;
    foreach ($result as $i => $row) {
      $body = $view->result[$i]->field_body[0]['rendered']['#markup'];
      $strong = $view->exposed_data['body_value'];
      $view->result[$i]->field_body[0]['rendered']['#markup'] = str_replace($strong, '<strong>' . $strong . '</strong>', $body);

Example for body field. Into your custom module.


You could use a javascript plugin like mark.js.

Here's an example with jQuery :


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.