|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 (permalink) |
|
Messages: n/a
Hébergeur: |
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}' > > |
|
|
|
#3 (permalink) |
|
Messages: n/a
Hébergeur: |
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/ |
|
|
|
#4 (permalink) |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 (permalink) |
|
Messages: n/a
Hébergeur: |
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/ |
|
|
|
#7 (permalink) |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#8 (permalink) |
|
Messages: n/a
Hébergeur: |
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/ |
|
![]() |
| Outils de la discussion | |
|
|