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 に答える