Re: php grep
- From: Bent Stigsen <ngap@xxxxxxxxxx>
- Date: Wed, 20 Apr 2005 02:11:20 +0200
foodigi wrote:
i am looking for a way to grep through a LARGE text file quickly and efficiently. i need to pull out just one line of the file. i have found the most efficient method so far to be:
$handle = popen('grep regex /path/to/file.txt', 'r'); $output = fread($handle, 2096); pclose($handle);
i have used most other methods of reading text in php, but they have proved to be very inefficient, and security is a major issue. any help with this subject will be much appreciated.
If you cant touch the file (chunk it down, transform it), then I seriously doubt you will find anything faster than using grep the way you do.
If you really are only interested in one line then add --max-count=1 as argument to grep. ('grep --max-count=1 regex /path/to/file.txt')
Then it will skip the rest as soon as the first line is found.
Also might be easier to use the exec command like:
$the_line = exec('grep...', $lines, $rtn_code);Which as a bonus, will give you the return code, which allways is a good thing to check, if you want to distinguish between an expression not found and an error.
And finally, regarding security, if "regex" is based on some user-input, you might want to do something like:
$arg = escapeshellarg( $user_input );
$cmd = "grep --max-count=1 $arg /path/to/file.txt";
/Bent .
- References:
- php grep
- From: foodigi
- php grep
- Prev by Date: php grep
- Next by Date: Re: smtp auth login
- Previous by thread: php grep
- Index(es):
Relevant Pages
|