COOL! --> most popular searched words
| From: | Derek Sivers | Date: | Fri, 01 Dec 2000 09:40:01 +0000 |
| Subject: | COOL! --> most popular searched words | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-28178@lists.php.net to get a copy of this message | ||
Hey PHP list -
I finally found the best/easiest way to find out what the most popular searched words were on our site!
Make each word the KEY in an array, and increase it's VALUE by 1 each time you run across it in your search logs!
Then just arsort the array, and voila!
Hope you find the following PHP code (from this newbie) valuable...
-------- most popular searched words ---------------------------------------------------
<html><head><title>most popular searched words</title></head>
<body>
<?
include "mysql.inc";
$db = new MySQL;
/* fluff words we want to exclude from reporting search results */
$fluff = array ("a", "the", "and", "of", "in", "to", "i", "on", "for", "it", "is");
/* show me every phrase that's ever been typed into our search box */
$query = "select allsearch from searched";
$db->Query($query);
while ($searched=$db->next_record())
{
/* separate phrases into individual words, remove puntuation, and lowercase it */
$searched_words = explode (" ", $searched[allsearch]);
while (list($key,$value) = each($searched_words))
{
$search_word = strtolower(eregi_replace("[^[:alnum:]]","",$value));
/* if it's not a fluff word, increase the counter number in an array with that word as the key! */
if (($search_word != "") AND (!in_array($search_word, $fluff)))
{
$searched_array[$search_word]++;
}
}
}/* sort our big array from highest counter numbers to lowest */ arsort($searched_array); reset($searched_array); /* show every word that was searched for more than 20 times, most popular first */ while ((list ($word, $hits) = each ($searched_array)) AND ($hits > 20))
{
print $word . " - " . $hits . "<br>\n";
}
?>
</body></html>
------------------------ (by Derek Sivers) ------------------------