|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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> |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On 9/12/07, Frank J. Schima <fjs@foraker.com> wrote:
> 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? it's sloppy but you could use system("wc -l $file") and grab the first part before the space (doesn't seem to be an option to NOT display the filename again) |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
hey...
if you're going to do the system approach, go ahead and use awk/cut... wc -l foo.txt | awk -F' ' '{print $1}' should work assuming you're on linux..... -----Original Message----- From: mike [mailto:mike503@gmail.com] Sent: Wednesday, September 12, 2007 12:46 PM To: Frank J. Schima Cc: php-general@lists.php.net Subject: Re: [php] Getting line count of a text file On 9/12/07, Frank J. Schima <fjs@foraker.com> wrote: > 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? it's sloppy but you could use system("wc -l $file") and grab the first part before the space (doesn't seem to be an option to NOT display the filename again) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Frank J. Schima wrote:
> 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? $f=fopen( yourfile, "r" ); $i=0; while( fgets($f) ) $i++; fclose($f); $count=$i; /Per Jessen, Zürich |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Hi Gang,
On Sep 12, 2007, at 1:58 PM, bruce wrote: > if you're going to do the system approach, go ahead and use awk/cut... > > wc -l foo.txt | awk -F' ' '{print $1}' > > should work assuming you're on linux..... I have solved the problem with this approach. Thanks for all the suggestions. FYI, it was a (very) minor pain to make that into a string. I ended up using this: $count = system("wc -l /path/to/myfile.txt | awk -F' ' '{print " . '$' . "1}'"); I didn't need to escape anything because the file was hardcoded - no user input. Cheers, Frank Schima Foraker Design <http://www.foraker.com> 303-449-0202 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Frank J. Schima 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> > > > echo intval(str_replace(' '.$file, '', trim(system('wc -l '.$file)))); -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare |
|
![]() |
| Outils de la discussion | |
|
|