This article mainly introduces the PHP programming calculation file or array of words in the frequency of the method, gives 2 statistical word frequency examples, involving PHP regular, array manipulation and string traversal and other related techniques, the need for friends can refer to the following
The examples in this paper are the methods for calculating the frequency of words in a PHP programming file or array. Share to everyone for your reference, as follows:
If it is a small file, it can be read into the array at once, using a handy array count function for Word frequency statistics (assuming that the contents of the file are separated by spaces):
<?php$str = file_get_contents ("/path/to/file.txt"); Get string from Filepreg_match_all ("/\b (\w+[-]\w+) | ( \w+) \b/", $str, $r); Place words into array $r-this includes hyphenated words$words = Array_count_values (Array_map ("Strtolower", $r [0])); Create new Array-with case-insensitive Countarsort ($words); Order from high to Lowprint_r ($words)
If it is a large file, read into memory is not appropriate, you can use the following methods:
<?php$filename = "/path/to/file.txt"; $handle = fopen ($filename, "R"); if ($handle = = = False) {exit;} $word = "", while (false!== ($letter = fgetc ($handle))) {if ($letter = = ") { $results [$word]++; $word = "";} else { $word. = $letter;}} Fclose ($handle);p rint_r ($results);
For large files, the second method is faster and more secure, without causing a memory exception.