- Forums
- PHP
- How To Highlight Searched Keywords In Search Results Form Wit PHP
This Page Contains information about How To Highlight Searched Keywords In Search Results Form Wit PHP By wallpaperama in category PHP with 5 Replies. [1021], Last Updated: Sat May 18, 2024
wallpaperama
Mon Apr 30, 2007
5 Comments
2042 Visits
if you've visit your favorite search engine, you have noticed that whenever you type a search keyword, the keywords you entered usually are highlighted to make it easy for you to see the keywords you typed to search for and usually it give you bunch of links of pages where those keywords are found.
well now you can make your own search engine on your website with PHP.
Thanks to the folks at webune for their help on this question. if you need PHP hosting they have excellent support. i recommend them.
so, here is the function to make all this happene.
enjoy
<?
#############################################################################################
############################### BOLD KEYWORD SEARCH ENGINE LIKE ############################
#############################################################################################
/**
* Perform a simple text replace
* This should be used when the string does not contain HTML
* (off by default)
*/
define('STR_HIGHLIGHT_SIMPLE', 1);
/**
* Only match whole words in the string
* (off by default)
*/
define('STR_HIGHLIGHT_WHOLEWD', 2);
/**
* Case sensitive matching
* (off by default)
*/
define('STR_HIGHLIGHT_CASESENS', 4);
/**
* Overwrite links if matched
* This should be used when the replacement string is a link
* (off by default)
*/
define('STR_HIGHLIGHT_STRIPLINKS', 8);
/**
* Highlight a string in text without corrupting HTML tags
*
* @author Aidan Lister
* @version 3.1.1
* @link http://aidanlister.com/repos/v/function.str_highlight.php
* @param string $text Haystack - The text to search
* @param array|string $needle Needle - The string to highlight
* @param bool $options Bitwise set of options
* @param array $highlight Replacement string
* @return Text with needle highlighted
*/
function str_highlight($text, $needle, $options = null, $highlight = null)
{
// Default highlighting
if ($highlight === null) {
$highlight = '\1';
}
// Select pattern to use
if ($options & STR_HIGHLIGHT_SIMPLE) {
$pattern = '#(%s)#';
$sl_pattern = '#(%s)#';
} else {
$pattern = '#(?!<.*?)(%s)(?![^<>]*?>)#';
$sl_pattern = '#(%s)#';
}
// Case sensitivity
if (!($options & STR_HIGHLIGHT_CASESENS)) {
$pattern .= 'i';
$sl_pattern .= 'i';
}
$needle = (array) $needle;
foreach ($needle as $needle_s) {
$needle_s = preg_quote($needle_s);
// Escape needle with optional whole word check
if ($options & STR_HIGHLIGHT_WHOLEWD) {
$needle_s = '\b' . $needle_s . '\b';
}
// Strip links
if ($options & STR_HIGHLIGHT_STRIPLINKS) {
$sl_regex = sprintf($sl_pattern, $needle_s);
$text = preg_replace($sl_regex, '\1', $text);
}
$regex = sprintf($pattern, $needle_s);
$text = preg_replace($regex, $highlight, $text);
}
return $text;
}
?>
you can use this to call the function for example:
echo str_highlight($searched_title, $search_keyword);
let me know if this helps