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

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

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."&nbsp;</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]."&nbsp;</div>";
}
}


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

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


效果是这样的:

  

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5646424.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存