【发布时间】: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 次的单词? - 请解释。告诉我们您期望的输出。