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

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

PHP中文网
Release: 2016-05-25 17:09:33
Original
1016 people have browsed it

<?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);
    }
}
Copy after login


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template