Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Sign In
Not a member yet? Click here to register.
highlight_words
This function will highlight the $word in the $string. This function is used in the search when highlighting words after a search.

highlight_words

Quote

highlight_words ( array $word, string $subject )


Parameters
word
The words to highlight, this must be an array.

subject
The text which contain the word that should be highlighted

Return Values
This function will return the $word highlighted in the $subject

Example 1
Code
<?php
$words = array (
 0 => "lemon",
 1 => "banana"
);
$subject = "banana, apple, orange and lemon";
 
echo highlight_words($words, $subject);
?>

Here you see the words to highlight already in an array given by the variable $words.

Output to Example 1

Quote

<span style='background-color:yellow;font-weight:bold;padding-left:2px;padding-right:2px'>banana</span>, apple, orange and <span style='background-color:yellow;font-weight:bold;padding-left:2px;padding-right:2px'>lemon</span>


Example 2
Code
<?php
$word = "lemon banana";
$words = explode(" ", $word);
$subject = "banana, apple, orange and lemon";
 
echo highlight_words($word1, $subject);
?>

We can't search for the $word as it is not an array, we therefore need to break it up and best way to do that is using the explode() function and store the result in the $words variable.

Output to Example 2

Quote

<span style='background-color:yellow;font-weight:bold;padding-left:2px;padding-right:2px'>banana</span>, apple, orange and <span style='background-color:yellow;font-weight:bold;padding-left:2px;padding-right:2px'>lemon</span>


Changelog
7.01.00 - New and optimised way to highlight words

Notes
The $word parameter must be an array!


See also …


References