Home  >  Article  >  Backend Development  >  How to calculate the frequency of words in a file or array using PHP

How to calculate the frequency of words in a file or array using PHP

怪我咯
怪我咯Original
2017-07-04 13:32:351156browse

This article mainly introduces the method of PHP programming to calculate the frequency of words in files or arrays, and gives 2 examples of counting word frequencies, involving PHP regularization, array operations and String traversal, etc. For related tips, friends who need them can refer to

. The example in this article describes the method of calculating the frequency of words in files or arrays through PHP programming. Share it with everyone for your reference, the details are as follows:

If it is a small file, it can be read into the array at one time, and the convenient array counting function can be used for word frequency statistics (assuming that the contents in the file are all separated by spaces) words):

<?php
$str = file_get_contents("/path/to/file.txt"); //get string from file
preg_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 count
arsort($words); //order from high to low
print_r($words)

If it is a large file, it is not appropriate to read it into the memory. You can use the following method:

<?php
$filename = "/path/to/file.txt";
$handle = fopen($filename,"r");
if ($handle === false) {
 exit;
}
$word = "";
while (false !== ($letter = fgetc($handle))) {
 if ($letter == &#39; &#39;) {
  $results[$word]++;
  $word = "";
 }
 else {
  $word .= $letter;
 }
}
fclose($handle);
print_r($results);

The above is the detailed content of How to calculate the frequency of words in a file or array using PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn