A detailed explanation of how frequently the words appear in an array of PHP implementations

Source: Internet
Author: User
Tags php programming
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.