首頁 >php教程 >PHP源码 >搜索引擎来源关键字分析程序

搜索引擎来源关键字分析程序

PHP中文网
PHP中文网原創
2016-05-25 17:09:33995瀏覽

<?php
/**
 * 分析搜索引擎到来的关键字
 * 
 * 
 * @author zhangjun
 * @charset utf-8
 */
class searchTerms {
    public static $banUrl = array(&#39;qq.com&#39;,&#39;localhost&#39;); //不解析的referer 有利于加快程序处理速度
    public static $parseHost = array(&#39;baidu&#39;,&#39;google&#39;,&#39;360&#39;,&#39;soso&#39;);  //能解析的referer
    /**
     * 主方法
     * @return boolean || array
     */
    public static function keyword(){
        if(!isset($_SERVER[&#39;HTTP_REFERER&#39;]))
            return false;
        $referer = trim($_SERVER[&#39;HTTP_REFERER&#39;]);
        $refererArr = parse_url($referer);
        //判断refer是否来至不需要分析的地址。
        if(self::inBanUrl($refererArr[&#39;host&#39;])) 
            return false;
        $hasParseFun = false;
        foreach(self::$parseHost as $host) {
            if(strpos ($refererArr[&#39;host&#39;],$host) !== false) { 
                $hasParseFun = true;
                break;
            }   
        }
        if(!$hasParseFun)
            return false;
        $queryVars = array();
        parse_str($refererArr[&#39;query&#39;], $queryVars);
        //调用每个搜索引擎的单独处理方法
        $method = &#39;parse&#39;.ucfirst($host);
        return self::$method($queryVars);
    }
    /* 已取消使用正则的方式。
    public static function buldPattern($rule) {
        $host = str_replace(&#39;.&#39;, &#39;.&#39;, $rule[&#39;host&#39;]);
        $patterns = array();
        foreach ($rule[&#39;key&#39;] as $key) {
            $patterns[] = $host . &#39;.+?&#39;.$key.&#39;=&#39;;
        } 
        return "/(?:" . implode(&#39;|&#39;, $patterns).&#39;)([^&]*)/&#39;;
    }*/
    public static function inBanUrl($referer) {
        foreach(self::$banUrl as $url) {
            if(strpos($url, $referer) !== false) 
                return true;
        }
        return false;
    }
    public static function parseBaidu($params)
    {
        $searchTerms = &#39;&#39;;
        if(isset($params[&#39;kw&#39;])) {
            $searchTerms = $params[&#39;kw&#39;];
        } else if(isset ($params[&#39;wd&#39;])) {
            $searchTerms = $params[&#39;wd&#39;];
        } else if(isset ($params[&#39;word&#39;])) {
            $searchTerms = $params[&#39;word&#39;];
        }
        return isset($params[&#39;ie&#39;]) && (strtolower($params[&#39;ie&#39;]) == &#39;utf-8&#39;) ? $searchTerms : iconv(&#39;gbk&#39;, &#39;utf-8&#39;, $searchTerms);
    }
    public static function parseGoogle($params)
    {
        $searchTerms = &#39;&#39;;
        if(isset($params[&#39;q&#39;])) {
            $searchTerms = $params[&#39;q&#39;];
        }
        return isset($params[&#39;ie&#39;]) && ($params[&#39;ie&#39;] == "GB") ? iconv(&#39;gbk&#39;, &#39;utf-8&#39;, $searchTerms) : $params[&#39;q&#39;];
    }
    public static function parse360($params)
    {
        $searchTerms = &#39;&#39;;
        if(isset($params[&#39;q&#39;])) {
            $searchTerms = $params[&#39;q&#39;];
        }
        return $searchTerms;
    }
    public static function parseSoso($params)
    {
        $searchTerms = &#39;&#39;;
        if(isset($params[&#39;w&#39;])) {
            $searchTerms = $params[&#39;w&#39;];
        }
        return iconv(&#39;gbk&#39;, &#39;utf-8&#39;, $searchTerms);
    }
}


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn