使用PHP的全动态关键字
我有一个完全动态的关键字脚本的想法,它允许我简单地为我的网站写内容/当人们在我的网站上发布,并从他们发布的内容自动生成关键字。。。我在下面探讨了这个方法,但我不确定如何前进。非常感谢您的帮助使用PHP的全动态关键字,php,html,Php,Html,我有一个完全动态的关键字脚本的想法,它允许我简单地为我的网站写内容/当人们在我的网站上发布,并从他们发布的内容自动生成关键字。。。我在下面探讨了这个方法,但我不确定如何前进。非常感谢您的帮助 <?php $content = "everything inside the body of the page"; $common = array(' a ', ' the ', ' I '); $replaced = str_replace($common, ' ',
<?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'])和$page['content']!=''){
foreach(数组)计数值(str\u单词)计数(str\u替换(数组(‘nbsp’、‘nbsp’、‘something’、‘that’、‘does’、‘that’、‘have’、‘with’、‘this’、‘from’、‘they’、‘will’、‘will’、‘that’、‘there’、‘when’、‘make’、‘like’、‘time’、‘just’、‘know’、‘take’、‘person’、‘into’、‘year除“,”之外“,”然后“,”看“,”只“,”过来“,”想“,”还“,”回去“,”工作“,”第一个“,”好“,”甚至“,”想要“,”因为“,”这些“,”给“,”大多数“,”,”,带标签($page['content']),1))作为$keyword=>$frequency){
如果($frequency>='3'和strlen($keyword)>='4'和strlen($keyword),您可以迭代数组并测试每个键的值,如果值足够高,则将该键作为值添加到新数组中
$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
现在保存您感兴趣的单词。考虑通过将单词添加到新数组来进行过滤。每次要将旧数组中的单词添加到新数组中时,请检查新数组中是否已存在该单词,如果该单词已存在,请使用if语句阻止其添加
<?php
// exclude words appearing more than this many times
$limit = 3;
// exclude these words
$wordsToExclude = array('a', 'the');
// the content
$content = "everything inside the body of the page a a a test test test test don't feed the elephants inside";
// better way of splitting into words - http://stackoverflow.com/questions/790596/split-a-text-into-single-words
$words = preg_split('/((^\p{P}+)|(\p{P}*\s+\p{P}*)|(\p{P}+$))/', $content, -1, PREG_SPLIT_NO_EMPTY);
// count how many times each word appears. this will create an array with words as the keys, and counts as the values
$uniqueWords = array_count_values($words);
foreach($uniqueWords as $word => $count)
{
// remove excluded words, and words appearing more times than the limit
if (in_array($word, $wordsToExclude) || $count > $limit) {
unset($uniqueWords[$word]);
}
}
var_dump($uniqueWords);
您可以只使用所有单词(使用数组值($uniqueWords)
),也可以使用计数作为某种形式的权重。只需添加
arsort($count);
并尽可能多地获取具有最大计数的键我如何才能筛选此数组中使用次数超过X的单词?-请解释。让我们了解您期望的输出。谢谢!这非常有用…我用我使用的解决方案更新了我的问题。
array (size=8)
'everything' => int 1
'inside' => int 2
'body' => int 1
'of' => int 1
'page' => int 1
'don't' => int 1
'feed' => int 1
'elephants' => int 1
arsort($count);