PHP Highlight Search Words Function

Nowadays if you are considering to develop a search engine, before or later, you have to face with highlight words within a text string problem. This can be useful for quickly finding relavant words withing large pages of text.
The following function will help you to highlight all the occurrences of the string and is case sensitive. The function is very simple and fast, it was written without using the regular expressions notoriously slow.

function highLightStr($originalString, $searchString){

$searchStringArray = explode(” “, $searchString);
for ($i=0;$i<sizeof($searchStringArray);$i++){
$bold_word='<b>’.$searchStringArray[$i].'</b>’;
$originalString = str_ireplace($searchStringArray[$i], $bold_word, $originalString);
}
return $originalString;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: