Re: Searching for a specific line within a file
- From: Jerry Stuckle <jstucklex@xxxxxxxxxxxxx>
- Date: Fri, 27 Jun 2008 21:02:07 -0400
Gene Kelley wrote:
Hello PHPers,
I'm trying to get at a single line within a text file. For instance, I want to print the line within a the file named "file.txt" that starts with "17:".
file.txt contains (condensed of course):
...
15: This is line 15
16: This is line 16
17: This is line 17 // <- This is the line I want to get
18: This is line 18
19: This is line 19
...
I'm able to get the FIRST line with:
<?php
$file = fopen("file.txt","r");
echo fgets($file);
fclose($file);
?>
Output:
1: This is line 1
Is there one function that might be out there to do this? and if so, what would be the most efficient way? Obviously, I'd like to avoid reading in the entire contents of the file (could get quite large) then say, explode()ing into an array, then searching the array for the contents of the line to print.
Am I even on the right track here? Oh, well... It's Friday!!! :-)
Thanx
Gene
Gene,
Either way, you need to read the file and search it. There isn't any PHP function to do that automatically, which leaves you the choice of reading the whole file in at once, or read it one line at a time, looking for the one you want.
Alternatively, if you can execute system utilities from PHP (many shared hosts disallow this), you could use grep to get the line(s).
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.
- References:
- Searching for a specific line within a file
- From: Gene Kelley
- Searching for a specific line within a file
- Prev by Date: Re: help with recusion in DOM
- Next by Date: Re: help with recusion in DOM
- Previous by thread: Re: Searching for a specific line within a file
- Next by thread: Re: Searching for a specific line within a file
- Index(es):
Relevant Pages
|