0

我无法使用此功能制作可点击的提取词:

$text = $curTemplate['name'];
function extract_keywords($str, $minWordLen = 4, $minWordOccurrences = 1, $asArray = false)
{
    function keyword_count_sort($first, $sec)
    {
        return $sec[1] - $first[1];
    }
    $str = preg_replace('/[^\p{L}0-9 ]/', ' ', $str);
    $str = trim(preg_replace('/\s+/', ' ', $str));

    $words = explode(' ', $str);
    $keywords = array();
    while(($c_word = array_shift($words)) !== null)
    {
        if(strlen($c_word) < $minWordLen) continue;

        $c_word = strtolower($c_word);
        if(array_key_exists($c_word, $keywords)) $keywords[$c_word][1]++;
        else $keywords[$c_word] = array($c_word, 1);
    }
    usort($keywords, 'keyword_count_sort');

    $final_keywords = array();
    foreach($keywords as $keyword_det)
    {
        if($keyword_det[1] < $minWordOccurrences) break;
        array_push($final_keywords, $keyword_det[0]);
    }
    return $asArray ? $final_keywords : implode(', ', $final_keywords);
}
//How to use

//Basic lorem ipsum text to extract the keywords
$text = "stackoverflow a language independent collaboratively edited question and answer site for programmers";

echo extract_keywords($text);

我想这样的话:

<a href="http://website.com/search.php?search=<?php echo extract_keywords($text); ?>"><?php echo extract_keywords($text); ?></a>

但不工作

以这种方式工作,但链接中没有关键字:(为什么????

<a href="http://website.com/search.php?search=NO KEY"><?php echo extract_keywords($text); ?></a>
4

1 回答 1

1

如前所述:

$keywords = extract_keywords($text, 4, 1, true);

foreach($keywords as $k => $keyword){
    echo '<a href="http://website.com/search.php?search=' .  $keyword . '">' . $keyword . '</a>' . ($k != (count($keywords) - 1) ? ',' : '');
}
于 2012-09-20T22:08:31.683 回答