Home  >  Article  >  Backend Development  >  Detailed explanation of how to count the frequency of words in an array using PHP

Detailed explanation of how to count the frequency of words in an array using PHP

巴扎黑
巴扎黑Original
2017-08-13 13:56:061922browse

This article mainly introduces the method of PHP programming to calculate the frequency of words in files or arrays. It gives 2 examples of counting word frequencies, involving PHP regularization, array operations, string traversal and other related skills. Friends in need You can refer to the following

The example of this article describes the method of calculating the frequency of words in a file or array using 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);

For large files, the second method is faster and safer and will not cause memory exceptions.

The above is the detailed content of Detailed explanation of how to count the frequency of words in an 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