Re: Getting line count of a text file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Sep 12, 2007, at 1:46 PM, Nathan Nobbe wrote:

On 9/12/07, Wolf <lonewolf@xxxxxxxxx> wrote:
$command        =  escapeshellcmd("cat {$file} | wc -l");
// Run command and capture results
$count  =  exec($command);


that would work on *nix, but not windows.

Since I am developing and deploying on *nix, shell commands are fine with me.

---- "Frank J. Schima" <fjs@xxxxxxxxxxx> wrote:
> Hi all,
>
>
> In PHP 5, I'm counting the number of lines in a text file using the
> following code:
>         $myfile = file ( '/path/to/myfile.txt');
>         $count = count ($myfile);
>
> However, sometimes it fails with the following error:
> PHP Fatal error: Allowed memory size of xxxx bytes exhausted (tried
> to allocate 35 bytes) in /path/to/myprogram.php on line 16
>
> Line 16 is the first line above.
>
> I'm sure there is a better - more memory efficient - way to do this.
> Any suggestions?
>
>
> Thanks!
> Frank Schima
> Foraker Design
> <http://www.foraker.com>
>
>

if you want a cross-platform solution (not that i use windows :))
or you dont want to use exec() or related functions look to the stream
functions. they support buffered reading, so theorectically a script could read portions of a file and count lines as it read through the portions.

Sounds a little painful. I'll look into it though.


-nathan


Thanks!

Frank Schima
Foraker Design
<http://www.foraker.com>
303-449-0202



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux