php - Counting the number of times a character occurs in a very large text file


I would like to count the occurrence of all commas in a very large text file (its has comma delimited data). Size is 28mb. I thought of loading up the text file and doing something like this:

substr_count($text, ',');

Good idea? Will it work?

The overall task is to find out how many rows of data it has. When I count the number of commas, I will divide this by the number of columns which will give me the number of rows. If there is a better way of doing this, let me know!

Thanks all


The below worked but is it efficient as the suggestions?

$lines = file('C:\wamp\CE.txt');

$number = 0;

foreach($lines as $line){

   $number = $number + substr_count($line, ',');


echo $number;



CSV goes like this:

lines -> rows
commas -> columns

So you got it wrong on the rows. If you want to count the rows just iterate the file for newlines.

$file = "youfile.txt"; 
$lines = count(file($file)); 
echo "There are ".$lines." lines in ".$file;

Would work as an example...




If your records are separated by new lines, you could do the following to get the number of rows:

  $file = "input.csv";
  $rows = count(file($file));



Good idea? Will it work?

Have you tried it? That should tell a lot.

Basically, sincesubstr_count is an intrinsic that does exactly what you want, it will very probably offer optimal performance.

If you find performance to be too bad, you might need to load the file in pieces, though.




Frankie is right in his answer. Hoever, I'd suggest reading the file with the file() function and then simply counting the array elements.

People are also looking for solutions to the problem: Should PHP code have spaces before operators?


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.