How to Reading, Counting Line, Words, and Characters in File With PHP
We have share in this tutorial how to Reading, Counting Line, Words, and Characters in File With PHP. First, read the file into a string with a function such as file_get_contents(). The numbers of words and characters can then be obtained by running the str_word_count() and strlen () function on the string.
"; // read file into array $array = file($file) or die("Can not read from file"); // count lines in the file echo "Counted " . count($array) ." line(s).
"; // count characters with space $numCharSpace = strlen($str); echo "Counted " .$numCharSpace. " character(s) with space.
"; // count characters without space $newStrs = preg_replace('/\s+/', '', $str); $numChar = strlen($newStrs); echo "Counted " .$numChar. " character(s) without space.
"; //counts words $numWords = str_word_count($str); echo "Counted " . $numWords . " word(s).
"; ?>