note 103294 added to function.str-word-count
| From: | matthewkastor at live dot com | Date: | Wed, 06 Apr 2011 02:08:15 +0000 |
| Subject: | note 103294 added to function.str-word-count | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178540@lists.php.net to get a copy of this message | ||
This needs improvement, but works well as is.
<?php
/**
* Generates an alphabetical index of unique words, and a count of their occurrences, in a file.
*
* This works on html pages or plain text files.
* This function uses file_get_contents, so it
* is possible to use a url instead of a local filename.
*
* Change the search pattern at
* <code> $junk = preg_match('/[^a-zA-Z]/', $word); </code>
* if you want to keep words with numbers or other characters. The pattern
* I've set searches for anything that is not an upper or lowercase letter,
* you may want something else.
*
* The array returned will look something like this:
* <code>
* Array
* (
* [0] => Array
* (
* [word] => a
* [count] => 21
* )
*
* [1] => Array
* (
* [word] => ability
* [count] => 1
* )
* )
* </code>
*
* @param string $file The file ( or url ) you want to create an index from.
* @return array
*/
function index_page($file) {
$index = array();
$find = array(
'/\r/',
'/\n/',
'/\s\s+/'
);
$replace = array(
' ',
' ',
' '
);
$work = file_get_contents($file);
$work = preg_replace('/[>][<]/', '> <', $work);
$work = strip_tags($work);
$work = strtolower($work);
$work = preg_replace($find, $replace, $work);
$work = trim($work);
$work = explode(' ', $work);
natcasesort($work);
$i = 0;
foreach($work as $word) {
$word = trim($word);
$junk = preg_match('/[^a-zA-Z]/', $word);
if($junk == 1) {
$word = '';
}
if( (!empty($word)) && ($word != '') ) {
if(!isset($index[$i]['word'])) { // if not set this is a new index
$index[$i]['word'] = $word;
$index[$i]['count'] = 1;
} elseif( $index[$i]['word'] == $word ) { // count repeats
$index[$i]['count'] += 1;
} else { // else this is a different word, increment $i and create an entry
$i++;
$index[$i]['word'] = $word;
$index[$i]['count'] = 1;
}
}
}
unset($work);
return($index);
}
?>
example usage:
<?php
$file = 'http://www.php.net/';
// or use a local file, see file_get_contents() for valid filenames and restrictions.
$index = index_page($file);
echo '<pre>'.print_r($index,true).'</pre>';
?>
----
Server IP: 69.147.83.197
Probable Submitter: 71.205.32.227
----
Manual Page -- http://www.php.net/manual/en/function.str-word-count.php
Edit -- https://master.php.net/note/edit/103294
Del: integrated -- https://master.php.net/note/delete/103294/integrated
Del: useless -- https://master.php.net/note/delete/103294/useless
Del: bad code -- https://master.php.net/note/delete/103294/bad+code
Del: spam -- https://master.php.net/note/delete/103294/spam
Del: non-english -- https://master.php.net/note/delete/103294/non-english
Del: in docs -- https://master.php.net/note/delete/103294/in+docs
Del: other reasons-- https://master.php.net/note/delete/103294
Reject -- https://master.php.net/note/reject/103294
Search -- https://master.php.net/manage/user-notes.php