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 > Getting line count of a text file
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Getting line count of a text file

Réponse
 
LinkBack Outils de la discussion
Vieux 12/09/2007, 20h35   #1
Frank J. Schima
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Getting line count of a text file

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
mike
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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)
  Réponse avec citation
Vieux 12/09/2007, 20h58   #3
bruce
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [PHP] Getting line count of a text file

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
  Réponse avec citation
Vieux 12/09/2007, 21h23   #4
Per Jessen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Getting line count of a text file

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
  Réponse avec citation
Vieux 12/09/2007, 21h37   #5
Frank J. Schima
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Getting line count of a text file

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



  Réponse avec citation
Vieux 12/09/2007, 21h40   #6
Jim Lucas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Getting line count of a text file

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
  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 17h32.


É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,11618 seconds with 14 queries