|

How to Count the Number of Words in a File in PHP?

Counting the number of words in a file is useful in many programs. In this post, we will discuss how to count the number of words in a file in a PHP script.

In PHP standard library, we have function str_word_count($str) which returns the number of words from a string $str. On the other hand, regarding files, we can open a file and read the content using PHP standard library functions fopen() and fgets(). Based on these, we can build a function to count the number of words in a file in PHP.

function get_word_cnt($file_fullpath) {
  $file = @fopen($file_fullpath, "r");
  if ($file === FALSE) {
    // error_log("Failed to open file $file_fullpath .");
    return -1;
  }
  $cnt = 0;
  while (!feof($file)) {
    $line = fgets($file, 8192);
    $cnt += str_word_count($line);
  }
  fclose($file);
  return $cnt;
}

Here, we read the file content in a while loop piece by piece. The fgets() function can not get an arbitrary large content. Otherwise, the PHP program will need quite a large memory for the $line object.

If there are failures, this function returns -1. Otherwise, it returns the number of words in the file.

Similar Posts

  • How to improve ssh/scp performance on Linux?

    ssh/scp are convenient and handy tools on Linux. Is is possible to further improve its speed/performance? Please check this post for how to improve ssh/scp performance: https://www.systutorials.com/5450/improving-sshscp-performance-by-choosing-ciphers/ Read more: Improving ssh/scp Performance by Choosing Suitable Ciphers Open Source and Portable SSH, SCP, SFTP and VNC Clients for Windows to Remote Control Linux How to improve…

  • Installing akmod NVIDIA Driver on Fedora

    Installing akmod NVIDIA driver from rpmfusion on Fedora 12. First make sure your nvidia card is in “Supported NVIDIA GPU Products List”: Click here for the list 1. Add rpmfusion repository: Enable RPM Fusion repositories 2. Install akmod nvidia driver akmod builds the required kmod on bootup # yum install akmod-nvidia xorg-x11-drv-nvidia-libs.i686 3. Add nouveau…

  • How to get an environment variable in Python?

    In Python, how to get an environment variable? In Python, you may use this piece of code to get an environment variable: os.environ.get(‘ENV_MIGHT_EXIST’) or this piece of code: os.getenv(‘ENV_MIGHT_EXIST’) It will return None if the environment variable is not present. Reference and for more ways, please check https://www.systutorials.com/dtivl/13/how-to-get-an-environment-variable?show=80#answer-80 . Read more: How to get an…

  • How to filter bbpress content in WordPress

    In WordPress, we can filter the post content by adding a filter to the_content: add_filter('the_content', 'inline_markdown_pre_process_shortcode2', 7); But how to filter the content for bbpress contents? bbpress topic and reply (they are separated) content have their own filters: bbp_get_topic_content and bbp_get_reply_content. For example, add_filter(‘bbp_get_topic_content’, ‘my_plugin_custom_bbp_function’); add_filter(‘bbp_get_reply_content’, ‘my_plugin_custom_bbp_function’); function my_plugin_custom_bbp_function($content) { return ‘my_plugin_custom_bbp_function processed: ‘.$content; }…

  • |

    How to enable user themes in Ubuntu 18.04?

    The way for Ubuntu 17 to installing the gnome-shell-extensions package does not work any more for Ubuntu 18.04. How to enable user themes in Ubuntu 18.04? The updated gnome-shell-extensions package actually adds the User Theme extension back. You can use that. First, install the package sudo apt install gnome-shell-extensions Second, log out and login again…

Leave a Reply

Your email address will not be published. Required fields are marked *