TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-18-2008, 04:31 AM   #1 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Application If, line 5 of file test.txt = "color"..do...else do ...

Good day to you, I woking on a script which is customizable for the web editor to change some option on the way some item on the page are displayed. My question is: How can I put in my code something that would do : if in file "setting.txt" row 5, the word "colored" appears do this piece of code else , do this piece. This would be within a function. Thanks !
__________________
That's why we are not alone on earth... let's build !
Peuplarchie is offline  
Reply With Quote
Old 11-18-2008, 09:42 AM   #2 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Oh, nevermind. Misread.
__________________
Tanax is offline  
Reply With Quote
Old 11-18-2008, 11:39 AM   #3 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

well, normally, you would read a file line by line. You can use a counter to determine the current line you're on, and test that line against the string "colored" to see if it is present or not.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 11-18-2008, 11:45 AM   #4 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

PHP Code:
<?php
    $settings 
= @file('/path/to/settings/file.txt');

    if(!
$settings)
    {
        die(
'Unable to read file');
    }

    if(
strtolower($settings[4]) == 'colored')
    {
        
/* then do */
    
}
    else
    {
        
/* else do */
    
}
?>
something like that should do I guess :)
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 11-18-2008, 03:20 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Smile

I'd be inclined to use an INI file, so that it's easily formatted for anybody to edit, and then create a base class to handle the core stuff, and then extend onto it for customisation colours, customisation sizes, etcetera.

Something like the following:

php Code:
include_once 'Customisation.php';
include_once 'Customisation_Colour.php';

$pCustomisation = new Customisation();
$pColour = new Customisation_Colour();

echo $pColour->forHeaderFont();

(Please see the attached file, also.)
Attached Files
File Type: rar Customisation.rar (861 Bytes, 9 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-19-2008, 02:05 AM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

In keeping with your style, however, I have created this for you, with a little debug functionality as well, just in case you require it.

php Code:
class Customisation
{
    private static $m_aData;
    const THROW_EXCEPTIONS = false;
   
    public static function getLine($iLine)
    {
        if (empty(self::$m_aData))
        {
            self::$m_aData = file('setting.txt');
        }
       
        if (!isset(self::$m_aData[$iLine - 1]))
        {
            if (self::THROW_EXCEPTIONS)
            {
                throw new Exception(sprintf('Line %d does not exist', $iLine));
            }
           
            return null;
        }
       
        return self::$m_aData[$iLine - 1];
    }
}

echo Customisation::getLine(3);

Then you can use it as so:

php Code:
if (Customisation::getLine(5) == 'colored')
{
    echo 'Do this';
}
else
{
    echo 'Do something else';
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-19-2008, 02:46 AM   #7 (permalink)
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
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:15 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design