Google hilite.

git-svn-id: http://svn.automattic.com/wordpress/trunk@747 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
saxmatt 2004-01-09 11:16:10 +00:00
parent 8a5c380f95
commit 598c4723cf
1 changed files with 56 additions and 0 deletions

View File

@ -1661,6 +1661,62 @@ function add_filter($tag, $function_to_add) {
return true;
}
/* Highlighting code c/o Ryan Boren */
function get_search_query_terms($engine = 'google') {
$referer = urldecode($_SERVER[HTTP_REFERER]);
$query_array = array();
switch ($engine) {
case 'google':
// Google query parsing code adapted from Dean Allen's
// Google Hilite 0.3. http://textism.com
$query_terms = preg_replace('/^.*q=([^&]+)&?.*$/i','$1', $referer);
$query_terms = preg_replace('/\'|"/','', $query_terms);
$query_array = preg_split ("/[\s,\+\.]+/", $query_terms);
break;
}
return $query_array;
}
function is_referer_search_engine($engine = 'google') {
$referer = urldecode($_SERVER[HTTP_REFERER]);
if ( ! $engine ) {
return 0;
}
switch ($engine) {
case 'google':
if (preg_match('|^http://(www)?\.?google\.*|i',$referer)) {
return 1;
}
break;
}
return 0;
}
function hilite($text) {
$search_engines = array('google');
foreach ($search_engines as $engine) {
if ( is_referer_search_engine($engine) ) {
$query_terms = get_search_query_terms($engine);
foreach ($query_terms as $term) {
if (!preg_match('/<.+>/',$text)) {
$text = preg_replace('/(\b'.$term.'\b)/i','<span class="hilite">$1</span>',$text);
} else {
$text = preg_replace('/(?<=>)([^<]+)?(\b'.$term.'\b)/i','$1<span class="hilite">$2</span>',$text);
}
}
break;
}
}
return $text;
}
// Check for hacks file if the option is enabled
if (get_settings('hack_file')) {
if (file_exists(ABSPATH . '/my-hacks.php'))