note 44479 deleted from function.str-replace by tomsommer
| From: | tomsommer@php.net | Date: | Sat, 07 Aug 2004 05:35:52 +0000 |
| Subject: | note 44479 deleted from function.str-replace by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-74136@lists.php.net to get a copy of this message | ||
Note Submitter: rehfeld.us
----
This will count all the words in a document and tally the results. all you SEO freaks will prob like
this :).
Its not perfect but its probabaly adequete for most peoples purposes, and its compact and fast.
default case insensitive
default http protocol
default sort descending frequency - optional alphabetical descending
uncomment the appropriate lines where commented to change it
##########################
<?php
function word_count($file) {
$text = strip_tags(file_get_contents($file));
$text = strtolower($text); // comment this line out to count words case sensitive
$strip = array(' ', "\r", "\n", "\t", '
');
$text = str_replace($strip, ' ', $text);
$words = explode(' ', $text);
while (false !== ($word = next($words))) {
if (!isset($results[$word])) $results[$word] = 1;
$results[$word] = $results[$word] +1;
}
return $results;
}
?>
################################
Example usage
################################
<form method="get" action="<?php echo $_SERVER['PHP_SELF'];
?>">
<p><label for="url"><input type="text" name="url"
id="url" value="<?php echo $_GET['url']; ?>" /> URL of
webpage</label></p>
<p><label for="submit"><input type="submit"
name="submit" id="submit" value="submit" /></label></p>
</form>
<pre>
<?php
if ($file = $_GET['url']) {
$file = (strstr($file, 'http://')) ? $file : 'http://'.$file; // comment this line out to prevent it from
adding " http:// "
$results = word_count($file);
// ksort($results);
arsort($results);
print_r($results);
}
?>