Forum Moderators: coopster

Message Too Old, No Replies

get string from file by line

         

scorpion

7:57 pm on Apr 24, 2003 (gmt 0)

10+ Year Member



Does anybody know how to open a file such that you can retrieve each line using a number?

e.g. a[1] = string on the first line of the file
a[2] = string on the second line of the file, etc..

mavherick

8:07 pm on Apr 24, 2003 (gmt 0)

10+ Year Member



I don't think (could be wrong) that there's a built in way to do that, but you could try the following:

$i = 1;

while($fp &&!feof($fp))
{
$myArray[$i] = trim(fgets($fp));
$i++;
}

after that you can use the array and lookup any line numbers

Note: depending on line length (when size of a line is over a certain amount of kb), there might be issues (more info here -> fgets [php.net])

mavherick

[added]changed start of counter to 1[/added]

andreasfriedrich

8:24 pm on Apr 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In PHP [php.net] you could use file [php.net] to read an entire file into an array. In Perl [perl.com] you can simply use the angle operator in list context.

Andreas