php count line , count word , count character script

The Count Line Word Character in file script is used to count word and character and line from an file.

Demo :



Create File : 
  File Name : File_Name.txt
Counted 2 line(s).
Counted 574 character(s) with spaces.
Counted 574 character(s) without spaces.
Counted 91 words.
 

Example :

If you need to check count words from file and count character from file and count character with out spaces and count words with spaces in the above demo very useful and you first update file through form.

Code :

Following code is use to Count Line Word Character in file script core if you need to copy and use to your website.


<?php
  $file = 'File_Name.txt';
  $str = file_get_contents($file) or die ("Cannot read from file");
  $arr = file ($file) or die ("Cannot read from file");
  echo "Counted ". sizeof($arr) . " line(s).\n";

  $numCharsSpaces = strlen($str);
  echo "Counted $numCharsSpaces character(s) with spaces.\n";

  $newStr = ereg_replace('[[:space:]]+', '', $str);
  $numChars = strlen($newStr);
  echo "Counted $numChars character(s) without spaces.\n";

  $numWords = str_word_count($str);
  echo "Counted $numWords words.\n"; ?>

In the above example $file variable is used to assign for file name and sizeof() function is used to display the for count line and strlen() function is used to count the character with space and str_word_count() function is used to count for word.

Free Download Script :

If you need to count line , count word , count character script click and download from following link.

Download




Content