View Single Post
Old 11-19-2008, 02:46 AM   #7 (permalink)
Salathe
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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($fp10240);
    }
    
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

Salathe is offline  
Reply With Quote