【问题标题】:Fully dynamic keywords using PHP使用 PHP 的完全动态关键字
【发布时间】:2015-05-17 14:59:49
【问题描述】:

我有一个完全动态的关键字脚本的想法,它可以让我简单地为我的网站编写内容/当人们在我的网站上发帖并从他们发布的内容中自动生成关键字......我已经探索下面这个方法,但我不知道如何前进。非常感谢任何帮助。

<?php
     $content = "everything inside the body of the page";
     $common = array(' a ', ' the ', ' I ');
     $replaced = str_replace($common, ' ', strip_tags($content));
     $array = str_word_count($replaced, 1);
     $count = array_count_values( $array );
?>

该代码从页面中获取内容,从中剥离 HTML 标记,从所有内容中创建一个数组,其中每个单词都有一个值,表示它在页面中的使用次数。

我怎样才能过滤这个数组中使用超过 X 次的单词?

编辑:感谢 Jan 提供他们的解决方案,这对我需要做的事情非常有帮助,但最终还是稍微改变了它(不要太讨厌我,但我将它合并为一行以节省空间)。

if ( isset($page['content']) and $page['content'] != ' ' ) {
    foreach ( array_count_values(str_word_count(str_replace(array('nbsp', ' nbsp ', ' something ', ' that ', ' does ', 'that', ' that ', ' have ',' with', ' this ', ' from ', ' they ', ' will ', ' would ', ' there ', ' their ', ' what ', ' about ', ' which ', ' when ', ' make ', ' like ', ' time ', ' just ', ' know ', ' take ', ' person ', ' into ', ' year ', ' your ', ' good ', ' some ', ' could ', ' them ', ' other ', ' than ', ' then ', ' look ', ' only ', ' come ', ' over ', ' think ', ' also ', ' back ', ' after ', ' work ', ' first ', ' well ', ' even ', ' want ', ' because ', ' these ', ' give ', ' most '), ' ', strip_tags($page['content'])), 1)) as $keyword => $frequency ) {
        if ( $frequency >= '3' and strlen($keyword) >= '4' and strlen($keyword) <= '10' and strpos($keywords, $keyword) === false ) {
            $keywords .= strtolower($keyword).', ';
        }
    }
    echo '<meta name="keywords" content="'.trim($keywords, ", ").'"/>';
}

【问题讨论】:

  • 我怎样才能过滤这个数组中使用超过 X 次的单词? - 请解释。告诉我们您期望的输出。

标签: php html


【解决方案1】:

考虑通过将单词添加到新数组来进行过滤。每次要从旧数组中添加一个单词到新数组中时,请检查该单词是否已存在于新数组中,并使用 if 语句阻止它添加,如果它已经存在

【讨论】:

    【解决方案2】:

    只需添加

     arsort($count);
    

    并尽可能多地获取最大计数的键

    【讨论】:

      【解决方案3】:

      您可以遍历数组并测试每个键的值,如果它足够高,则将该键作为值添加到新数组中。

      $min_count = 1; // Number of times the word should be found inside the content to be considered as a keyword
      $keywords = array();
      foreach ( $count as $keyword => $value ) {
          if ( $value >= $min_count ) {
              $keywords[] = $keyword;
          }
      }
      

      $keywords 现在包含您感兴趣的单词。

      【讨论】:

      • 谢谢!这非常有用......我用我使用的解决方案更新了我的问题。
      猜你喜欢
      • 1970-01-01
      • 2016-05-22
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 2018-10-07
      相关资源
      最近更新 更多