RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Highlight Words in Search Results

When searching text for a specific keyword or keywords, it's good practice to give the user the option of deciding whether they'd like those keywords highlighted in search results. The following PHP function will highlight defined keywords in text by regular expression.

It will turn this:

Lorem ipsum dolor sit semper amet, consectetur adipiscing elit. Semper vestibulum eu nulla auctor, vehicula sem vel, suscipit lorem ligula. Quisque libero lorem, semper eget fermentum id, condimentum at turpis.

Into this:

Lorem ipsum dolor sit semper amet, consectetur adipiscing elit. Semper vestibulum eu nulla auctor, vehicula sem vel, suscipit lorem ligula. Quisque libero lorem, semper eget fermentum id, condimentum at turpis.

PHP Functions

This is the older function we used. PHP's ereg_replace() function was depreciated in version 5.3.0 meaning that it's not an ideal long-term solution.

1
<?php 
2
/*
3
 Highlight Words in Search Results
4
 http://www.beliefmedia.com/code/php-snippets/highlight-search
5
*/
6
 
7
function beliefmedia_highlight($text, $words, $color='yellow', $case = true) {
8
 
9
 $words = trim($words);
10
 $words_array = explode(' ', $words);
11
 
12
 foreach($words_array as $word) {
13
   if(strlen(trim($word)) != 0)
14
     $text = ($case !== false) ? eregi_replace($word, '<font style="background:' . $color . '";>\\0</font>', $text) : ereg_replace($word, '<font style="background:' . $color . '";>\\0</font>', $text);
15
   }
16
 
17
 return $text;
18
}

A more-up-to-date function is as follows.

1
<?php 
2
/*
3
 Highlight Words in Search Results
4
 http://www.beliefmedia.com/code/php-snippets/highlight-search
5
*/
6
 
7
function beliefmedia_highlights($text, $words, $case = false) {
8
 
9
 $words = trim($words);
10
 $words_array = explode(',', $words);
11
 
12
 $regex = ($case !== false) ? '/\b(' . implode('|', array_map('preg_quote', $words_array)) . ')\b/i' : '/\b(' . implode('|', array_map('preg_quote', $words_array)) . ')\b/';
13
 foreach($words_array as $word) {
14
  if(strlen(trim($word)) != 0)
15
   $text = preg_replace($regex, '<font style="background: yellow";>$1</font>', $text);
16
  }
17
 
18
 return $text;
19
}

Both are a little old. We'll share others soon.

Example

Using the last function, use the following:

1
<?php 
2
$words = 'lorem,semper,ipsum';
3
$text = 'Lorem ipsum dolor sit semper amet, consectetur adipiscing elit. Semper vestibulum eu nulla auctor, vehicula sem vel, suscipit lorem ligula. Quisque libero lorem, semper eget fermentum id, condimentum at turpis.';
4
echo beliefmedia_highlights($text, $words, $case = false);

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment