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, 21h07   #1 (permalink)
Wolf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [PHP] Getting line count of a text file

Novel approach... But mine's less typing.

never thought of using awk for it though. Good one!

---- bruce <bedouglas@earthlink.net> wrote:
> 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
>
> --
> 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, 21h19   #2 (permalink)
mike
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Getting line count of a text file

personally i try to use as little shell commands as possible,
especially stacked on top of each other in exec().

i'd just use explode, str_split, substr/strpos, etc. after i got it back.

On 9/12/07, Wolf <lonewolf@nc.rr.com> wrote:
> Novel approach... But mine's less typing.
>
> never thought of using awk for it though. Good one!
>
> ---- bruce <bedouglas@earthlink.net> wrote:
> > hey...
> >
> > if you're going to do the system approach, go ahead and use awk/cut...
> >
> > wc -l foo.txt | awk -F' ' '{print $1}'
> >

  Réponse avec citation
Vieux 12/09/2007, 22h12   #3 (permalink)
Greg Donald
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Getting line count of a text file

On Wed, 12 Sep 2007, mike wrote:
> personally i try to use as little shell commands as possible,
> especially stacked on top of each other in exec().


Why? Shell commands are a lot less likely to change than the
current PHP function names.

I'm aware the PHP function naming algorithm currently remains a
secret, but without a doubt, someday someone _will_ crack it. I
personally hope this occurs before the renaming of PHP to Java.


--
Greg Donald
Cyberfusion Consulting
http://cyberfusionconsulting.com/
  Réponse avec citation
Vieux 12/09/2007, 22h18   #4 (permalink)
mike
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Getting line count of a text file

On 9/12/07, Greg Donald <greg@cyberfusionconsulting.com> wrote:
> Why? Shell commands are a lot less likely to change than the
> current PHP function names.
>
> I'm aware the PHP function naming algorithm currently remains a
> secret, but without a doubt, someday someone _will_ crack it. I
> personally hope this occurs before the renaming of PHP to Java.


if the basic functions i mentioned above change names that one line of
code will be the least of everyone's concerns.

not to mention PHP maintains cross-platform support, whereas shell
commands wouldn't, shell commands may have inherent security risks,
disabled, not installed, selinux or some other weird limitations, etc.
PHP most likely wouldn't have specific basic functions disabled.
  Réponse avec citation
Vieux 13/09/2007, 01h10   #5 (permalink)
bruce
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [PHP] Getting line count of a text file

greg...

while you are correct in your assertion that the shell cmds provided are specific to linux, your assertion/statement that the functions might be 'turned off' are fud... basic bash functions are inherently part of the shell....

and yes, you are correct in your statement that php is relatively cross platform, however, i imagine that you can quite easily get into situations where your php code has to have different branches based upon the platform that it's running on...

every other language i've ever written for has required me to have subtle differences based upon the underlying platform, i'm pretty sure php would/can as well.... the assumption here, is that you're getting into complex stuff..

peace.


-----Original Message-----
From: mike [mailto:mike503@gmail.com]
Sent: Wednesday, September 12, 2007 2:18 PM
To: Greg Donald
Cc: php-general@lists.php.net
Subject: Re: [php] Getting line count of a text file


On 9/12/07, Greg Donald <greg@cyberfusionconsulting.com> wrote:
> Why? Shell commands are a lot less likely to change than the
> current PHP function names.
>
> I'm aware the PHP function naming algorithm currently remains a
> secret, but without a doubt, someday someone _will_ crack it. I
> personally hope this occurs before the renaming of PHP to Java.


if the basic functions i mentioned above change names that one line of
code will be the least of everyone's concerns.

not to mention PHP maintains cross-platform support, whereas shell
commands wouldn't, shell commands may have inherent security risks,
disabled, not installed, selinux or some other weird limitations, etc.
PHP most likely wouldn't have specific basic functions disabled.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
  Réponse avec citation
Vieux 13/09/2007, 03h32   #6 (permalink)
Chris
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Getting line count of a text file

bruce wrote:
> greg...
>
> while you are correct in your assertion that the shell cmds provided are specific to linux, your assertion/statement that the functions might be 'turned off' are fud... basic bash functions are inherently part of the shell....


Sure they can - enable safe-mode and it causes all sorts of weirdness
with running exec or system calls.

I also disable exec/system calls in php altogether by editing my php.ini
file (see disabled_functions).

Lastly wc and awk and sed are not part of the shell, they are external
programs.

Shell functions are things like if, while, do.

--
Postgresql & php tutorials
http://www.designmagick.com/
  Réponse avec citation
Vieux 13/09/2007, 05h28   #7 (permalink)
mike
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Getting line count of a text file

On 9/12/07, Chris <dmagick@gmail.com> wrote:

> bruce wrote:
> > greg...


actually, i was the one that said this.

> Sure they can - enable safe-mode and it causes all sorts of weirdness
> with running exec or system calls.


not to mention shell commands expect to be in PATH, and if you
hardcode in /bin/awk, it might not be the same location in freebsd vs.
linux vs. solaris vs. another unix. or a crippled or broken system.

> I also disable exec/system calls in php altogether by editing my php.ini
> file (see disabled_functions).
>
> Lastly wc and awk and sed are not part of the shell, they are external
> programs.


thank you.

> Shell functions are things like if, while, do.


exactly, thank you. your reply was much more eloquent than what i
would have said.
  Réponse avec citation
Vieux 13/09/2007, 18h48   #8 (permalink)
Greg Donald
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: [PHP] Getting line count of a text file

On Wed, 12 Sep 2007, bruce wrote:
> while you are correct in your assertion that the shell cmds provided are
> specific to linux,


What assertions? I didn't know I made one. It's common sense but I
don't recall making an assertion and then asking you if it was correct.

> your assertion/statement that the functions might be 'turned off' are
> fud... basic bash functions are inherently part of the shell....


You must be smoking crack dude, I never said anything about any PHP
functions being turned off. I said 'renamed'.

> and yes, you are correct in your statement that php is relatively
> cross platform,


What statement?

> however, i imagine that you can quite easily get into situations
> where your php code has to have different branches based upon the
> platform that it's running on...


Bzzzzt! Wrong. When the prospective employer asks me if I have any
questions, I always ask if I'll be forced to use or develop against or
touch any M$ products. If they say yes I get up and leave. It's quite
humorous the three times I've done it.

> every other language i've ever written for has required me to have
> subtle differences based upon the underlying platform, i'm pretty sure
> php would/can as well.... the assumption here, is that you're getting
> into complex stuff..


I haven't made any assumptions. Seems the other way around entirely.


--
Greg Donald
Cyberfusion Consulting
http://cyberfusionconsulting.com/
  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 03h27.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13367 seconds with 16 queries