//根据标题获得百度热门相关关键字,返回字符串
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 class=ls>(.*)</li><li class=kwc><a target=_blank href=(.*)>(.*)</a></li><li class=bar><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"));
$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;
}
SEO的几个函数,分词,优化(转载)
最新推荐文章于 2024-08-20 23:21:48 发布