11-19-2008, 02:46 AM
|
#7 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
I'd rather not have to load a whole file into memory (the OP didn't say how big it was) so here's something which only grabs what we're looking for. Of course, it's not perfect but might give you an new way of finding the right line in the file (compared to those above). It's basically putting xenon's post into code form.
PHP Code:
function fline($file, $pos)
{
if ( ! $fp = fopen($file, 'r'))
{
throw new RuntimeException('Unable to read file '.$file);
}
$line = NULL;
$lnum = 0;
while ($lnum++ < $pos && ! feof($fp))
{
$line = fgets($fp, 10240);
}
fclose($fp);
return $line;
}
// rtrim the line because fline includes a trailing newline
$line = rtrim(fline('settings.txt', 5));
if ($line == 'colored')
{
// Do something colourful
}
else
{
// Do something boring
}
|
|
|
|