PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Re: [PHP] Getting line count of a text file
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: [PHP] Getting line count of a text file

Réponse
 
LinkBack Outils de la discussion
Vieux 12/09/2007, 20h41   #1
Wolf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Getting line count of a text file

$command = escapeshellcmd("cat {$file} | wc -l");
// Run command and capture results
$count = exec($command);



---- "Frank J. Schima" <fjs@foraker.com> 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>
>
>

  Réponse avec citation
Vieux 12/09/2007, 20h46   #2
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Getting line count of a text file

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




that would work on *nix, but not windows.

---- "Frank J. Schima" <fjs@foraker.com> 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.

-nathan

  Réponse avec citation
Vieux 12/09/2007, 20h52   #3
Frank J. Schima
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Getting line count of a text file


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

> On 9/12/07, Wolf <lonewolf@nc.rr.com> 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@foraker.com> 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



  Réponse avec citation
Vieux 12/09/2007, 21h05   #4
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting line count of a text file

On Sep 12, 3:46 pm, quickshif...@gmail.com ("Nathan Nobbe") wrote:
>
> 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.
>
> -nathan


Right -- something like this:

$numLines = 0;

$handle = fopen('/path/to/myfile.txt', 'r');

while(!feof($handle)) {
fgets($handle);
++$numLines;
}

fclose($handle);

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h10.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12117 seconds with 12 queries