destoon根据标题调取百度下拉+淘宝下拉词,增强页面相关性

   写了一个destoon根据标题调取百度下拉+淘宝下拉词函数,增强页面相关性,也可以用于自动分词,提取相关词,废话不多说。上代码!

   首先在/api/extend.func.php里加入:

   

//根据标题调取相关下拉
function getRelcontentbyTitle($title){
$word=$title;
$word=urlencode($word);
$s = file_get_contents('http://suggest.taobao.com/sug?extras=1&code=utf-8&callback=g_ks_suggest_callback&q='.$word);
preg_match('/\{.+\}/', $s, $m);
foreach (json_decode($m[0])->result as $v) {
    $arr[] = $v[0];
}
//print_r($arr);
if ($arr!==""){
//获取数组中元素的值如下:
foreach($arr as $key=>$value) {
   echo  "<div  style='margin-right:3px;line-height:2.2;' class='label label-warning'  >".$value." </div>";
    }
}
$data=file_get_contents('http://suggestion.baidu.com/su?wd='.$word);
$data=mb_convert_encoding($data, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5' );
$data_temp=strpos($data,"x");
$data=substr_replace($data,"",$data_temp,17);
$data = trim($data,");");
$data = trim($data,"{");
$data=preg_replace("/q:.+?.e,/",'', $data);
$data = str_replace("[","",$data);
$data = str_replace("]","",$data);
$data = "[".$data."]";
$data = str_replace(",","},s:",$data);
$data = str_replace("s:","{\"s\":",$data);//复杂的处理,以符合json格式
$dc=json_decode($data);
for ($n=0; $n<=4; $n++)
{
$wd[$n]=$dc[$n]->s;
echo  "<div  style='margin-right:3px;line-height:2.2;' class='label label-warning'  >".$wd[$n]." </div>";
}
}

  然后在内容模板页面调用,例如:

  

<span class="glyphicon glyphicon-tags">相关联想: {getRelcontentbyTitle($title)}</span>

  

文章转自:http://www.kaotop.com/it/640618.html

  

千行代码,Bug何处藏。 纵使上线又怎样,朝令改,夕断肠。
原文地址:https://www.cnblogs.com/68xi/p/15648354.html