PHP中SEO的几个函数,分词,优化

//根据标题获得百度热门相关关键字,返回字符串
function baiduKeyword($title,$num=5,$charset=”UTF-8″){
       
        $title=iconv($charset, “GB2312”, $title);
        $w=file_get_contents(‘http://d.baidu.com/rs.php?q=’.urlencode($title).’&tn=baidu’);
        //die($w);
        //获得列表部分
        preg_match_all(“|<div id=con>(.*)</div>|isU”,$w,$con);
        $list=$con[1][0];
        //获得具体内容
        preg_match_all(“|<ul><li>(.*)</li><li><a target=_blank href=(.*)>(.*)</a></li><li><img src=http://img.baidu.com/img/bar_1.gif height=6 width=(\d*) align=absmiddle vspace=5></li></ul>|isU”,$list,$content);
        //合并成数组,被搜索次数做为键值
        $c=array_combine($content[4],$content[3]);
        //排序
        krsort($c);
        //取前N条
        $r=array_slice($c, 0, $num);   
        //转换成字符串
        $result=implode(“,”, $r);
        $result=iconv(“GB2312”, $charset,$result);
        return $result;
}

//通过DZ获取文章关键字,输入标题和内容 即可返回5个关键字数组
function getTags($title,$content){

        $subjectenc = rawurlencode(strip_tags($title));
        $messageenc = rawurlencode(strip_tags(preg_replace(“/\[.+?\]/U”, ”,$content)));
        $subjectenc =substr($subjectenc,0,60);
        $messageenc=substr($messageenc,0,1200);
        $data = @implode(”, file(“http://keyword.discuz.com/related_kw.html?title=$subjectenc&content=$messageenc&ics=utf-8&ocs=utf-8&#8221;));
       
        $kws = array();
        if($data) {
                $parser = xml_parser_create();
                xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
                xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
                xml_parse_into_struct($parser, $data, $values, $index);
                xml_parser_free($parser);
                foreach($values as $valuearray) {
                        if($valuearray[‘tag’] == ‘kw’ || $valuearray[‘tag’] == ‘ekw’) {
                                $kw =trim($valuearray[‘value’]);       
                                $kws[] =$kw ;

                        }
                }
        }
        return $kws;
}

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: