dd

php计算title标题相似比

jerry thinkphp 2015年11月18日 收藏
采集时常常要对网站内容进行重复判断,php判断正文内容比较困难,对于标题的判断还是比较简单,只要过滤标题中的版权,再用similar_text计算
<?php 
/*
 * php计算title标题相似比 by enenba.com
 *
 * @param string $title_1 题目1
 * @param string $title_2 题目2
 * @return float $percent 相似百分比
 */
function title_similar($title_1,$title_2) {
    $title_1 = get_real_title($title_1);
    $title_2 = get_real_title($title_2);
    similar_text($title_1, $title_2, $percent);
    return $percent;
}
/**
 * php采集文章题目并去版权
 * @param string  $html  需要采集的html源代码
 * @return string
 */

function get_real_title($str){
    $str = str_replace(array('-','—','|'),'_',$str);
    $splits = explode('_', $str);
    $l = 0;
    foreach ($splits as $tp){
        $len = strlen($tp);
        if ($l < $len){$l = $len;$tt = $tp;}
    }
    $tt = trim(htmlspecialchars($tt));
    return $tt;
}


//以下是测试
$title_1 = '代号PHPCMS V9产品正式发布公测版本 - 站长之家';
$title_2 = 'PHPCMS再战江湖 V9产品正式发布公测版本 - 站长网 admin5.com';
$percent = title_similar($title_1,$title_2);
echo '相似百分比:'.$percent.'%';
echo "<br />\n";

?> 
dd