获取搜索引擎关键词,然后针对性的显示匹配结果,对于提升用户访问友好性有很大的帮助。
php获取搜索引擎关键词的方法,网上有一些,但是并不全面,而使用正则表达式则可以简单的实现该功能。参见如下代码示例:
<?php header("Content-type:text/html;charset=utf-8"); function get_keyword($url, &$keywords) { $regex = "/(?:soso.+?w=|360.+?q=|baidu.+?wd=|baidu.+?kw=|baidu.+?word=|google.+?q=|sogou.+?query=|bing.+?q=|yahoo.+?[?|&]p=|lycos.+?query=|onseek.+?keyword=|search.tom.+?word=|search.qq.com.+?word=|zhongsou.com.+?word=|search.msn.com.+?q=|yisou.com.+?p=|sina.+?word=|sina.+?query=|sina.+?_searchkey=|sohu.+?word=|sohu.+?key_word=|sohu.+?query=|163.+?q=|Alltheweb.+?q=|115.+?q=|youdao.+?q=|bing.+?q=|114.+?kw=)([^&]*)/"; $matches = array(); if(preg_match($regex.'i', $url, $matches)){ $keywords = urldecode($matches[1]) . " "; $keywords = mb_convert_encoding($keywords, "UTF-8", "UTF-8,GB2312,GBK"); /* echo "<pre>", $matches[0]; echo "<br/>",$keywords; echo "</pre>"; */ return $keywords; } return false; } $url=isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'';//入站url /* $url = '#http://www.baidu.com/s?ie=utf-8&bs=php+%E6%AD%A3%E5%88%99&f=8&rsv_bp=1&wd=php+%E6%AD%A3%E5%88%99&inputT=0#'; $url = "#http://www.google.com.hk/#hl=zh-CN&newwindow=1&safe=strict&site=&source=hp&q=%E5%A4%A7%E5%AE%B6%E8%AF%B4%E7%9A%84%E5%AF%B9%E5%95%8A&oq=%E5%A4%A7%E5%AE%B6%E8%AF%B4%E7%9A%84%E5%AF%B9%E5%95%8A&gs_l=hp.3...1861.7721.0.8006.28.16.0.0.0.0.0.0..0.0...0.0..0.1c.z-hiCGyKPmE&bav=on.2,or.r_gc.r_pw.&fp=145e4b6e1c786820&biw=1440&bih=799#"; $url = "http://so.360.cn/s?ie=utf-8&src=hao_phome&q=%E5%BF%97%E6%96%87%E5%B7%A5%E4%BD%9C%E5%AE%A4"; $url = "http://www.soso.com/q?pid=s.idx&cid=s.idx.se&w=%D6%BE%CE%C4%B9%A4%D7%F7%CA%D2%B0%A1"; $url = "http://www.soso.com/q?sc=web&bs=%D6%BE%CE%C4%B9%A4%D7%F7%CA%D2%B0%A1&ch=w.uf&num=10&w=site%3Alzw.me+%D6%BE%CE%C4%B9%A4%D7%F7%CA%D2"; $url = "http://cn.bing.com/search?q=%E5%BF%97%E6%96%87%E5%B7%A5%E4%BD%9C%E5%AE%A4&go=&qs=n&form=QBLH&pq=%E5%BF%97%E6%96%87%E5%B7%A5%E4%BD%9C%E5%AE%A4&sc=0-0&sp=-1&sk="; $url = "http://cn.bing.com/search?q=site%3Alzw.me+%E5%BF%97%E6%96%87%E5%B7%A5%E4%BD%9C%E5%AE%A4&qs=n&form=QBRE&pq=site%3Alzw.me+%E5%BF%97%E6%96%87%E5%B7%A5%E4%BD%9C%E5%AE%A4&sc=0-0&sp=-1&sk="; $url = "http://www.sogou.com/web?query=site%3Alzw.me+%D6%BE%CE%C4%B9%A4%D7%F7%CA%D2+%B9%A4%D7%F7%CA%D2&_asf=www.sogou.com&_ast=1347285377&w=01019900&p=40040100&sut=10120&sst0=1347285376751"; //*/ $url = "http://search.yahoo.com/search;_ylt=Ale90GdGX6zXqXZkdrOFVeObvZx4?p=site%3Alzw.me+%E5%BF%97%E6%96%87%E5%B7%A5%E4%BD%9C%E5%AE%A4+%E5%B7%A5%E4%BD%9C%E5%AE%A4+%E5%BF%97%E6%96%87&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-701"; echo "<pre>"; get_keyword($url,$keywords); echo $keywords; echo "<br />"; $arr_keys = explode(" ",trim($keywords)); print_r($arr_keys); echo "</pre>"; ?>
我得自己加个好搜的
高手!
博客导航栏真的很好玩