我从文章主题标题创建标签云。我得到每个标题,分成单词并将它们放入一个数组中,检查单词 strlen > 3 而不是我的排除单词数组。这很好用....
我正在努力解决的问题是:
- 如何随机化顺序并将输出限制为 20
- 排除重复项,重复项是指重复的单词,但在同一个 catid 中。
例如,下面的单词 dog 在 3 个不同的 catid 中重复了 5 次。因此,我想为每个不同的 catid 输出单词 dog 3 次。大批:
'subject' => 'dog is running', 'id' => '1', 'catid' => '19'
'subject' => 'dog is walking', 'id' => '2', 'catid' => '18'
'subject' => 'dog is sitting', 'id' => '3', 'catid' => '18'
'subject' => 'dog is eating', 'id' => '4', 'catid' => '19'
'subject' => 'dog is barking', 'id' => '5', 'catid' => '20'
这是我的代码:
$excluded_word_array = array('a','blah','bleh');
// prepare the tag cloud array for display
$terms = array(); // create empty array
$query = mysql_query("SELECT * FROM hesk_kb_articles WHERE type = '0'");
while($row = mysql_fetch_array($query)){
$subject = $row['subject'];
$id = $row['id'];
$catid = $row['catid'];
$words = explode(" ", $subject);
foreach ($words as $val){
if (strlen($val) > 3) {
$stripped_val = strtolower(ereg_replace("[^A-Za-z]", "", $val));
if (!in_array($stripped_val, $excluded_word_array)) {
shuffle($stripped_val);
$terms[] = array('subject' => $stripped_val, 'id' => $id, 'catid' => $catid);
}
}
}
}
sort($terms);
?>