Skip to content

Instantly share code, notes, and snippets.

@isS
Created October 22, 2011 17:11
Show Gist options
  • Save isS/1306233 to your computer and use it in GitHub Desktop.
Save isS/1306233 to your computer and use it in GitHub Desktop.
Highlight search engine keywords for the landing page
/**
* 关键词高亮动态解析
*
* @author Saturn
* @since 1.0.0
* @return void
*/
var Highlighter={
highlight:
function(b,a){
b=b.replace(/(^\s*)|(\s*$)/g,"");
if(!b||b.length==0){
return;
}
var c=new RegExp("("+b.split(/\s+/).join("(?!;)|")+"(?!;))","ig");
if("undefined"==typeof(a)){return;}
for(i in a){
if(a[i].innerHTML){a[i].innerHTML=a[i].innerHTML.replace(c,'<span style="color:#c00;font-size:1em;font-weight:inherit">$1</span>');}
}
}
};
/**
* search_engine_hl
*
* 提取来自搜索引擎的关键词
*
*
* 目前支持的搜索引擎包括: Google/Baidu/Yahoo/游子站内搜索
*
* @return mixed
*/
function search_engine_hl()
{
$ref = ( ! isset($_SERVER['HTTP_REFERER']) OR $_SERVER['HTTP_REFERER'] == '')
? ''
: trim($_SERVER['HTTP_REFERER']);
// There is no referer, exit...
if( $ref == '') return FALSE;
// Parse the referer URL
parse_str(parse_url($ref , PHP_URL_QUERY) , $query);
// Filter function to prevent potential XSS attacks
$filter = function($match) {
return trim(strip_tags($match));
};
// Referer from Google, Yahoo and our own search engine...
foreach(array('q', 'p', 'query') as $key)
{
if(isset($query[$key]) AND $query[$key])
{
return $filter($query[$key]);
}
}
// Referer from Baidu, extra works should be done
if(isset($query['wd']) AND $query['wd'])
{
return $filter(iconv('gb2312', 'utf-8', urldecode($query['wd'])));
}
return FALSE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment