Page 1 of 1

If, line 5 of file test.txt = "color"..do...else do ...

Posted: Mon Nov 17, 2008 10:31 pm
by Peuplarchie
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 !

Re: If, line 5 of file test.txt = "color"..do...else do ...

Posted: Mon Nov 17, 2008 10:35 pm
by requinix

Code: Select all

$file = file("setting.txt");
if (strpos($file[4], "colored") !== false) {
    // this piece of code
} else {
    // this piece
}

Re: If, line 5 of file test.txt = "color"..do...else do ...

Posted: Mon Nov 17, 2008 11:28 pm
by Peuplarchie
Nice, small and sirect to the point..
I had to change the

Code: Select all

 
!== false
 
to

Code: Select all

 
!= true
 
and it's working !


How about adding a 3 or more options ?

like
if "underline" do this....
if "alternate" do that
as well as the other
the else and the "colored" ?

Re: If, line 5 of file test.txt = "color"..do...else do ...

Posted: Mon Nov 17, 2008 11:37 pm
by requinix
Your modification confuses me.

Code: Select all

if (strpos($line[4], "underline") !== false) { // has underline
} else if (strpos($line[4], "colored") !== false) { // has colored
} else if (strpos($line[4], "alternate") !== false) { // has alternate
} else { // has something else
}

Re: If, line 5 of file test.txt = "color"..do...else do ...

Posted: Tue Nov 18, 2008 12:12 am
by Peuplarchie
why do you say it confused you ?

Re: If, line 5 of file test.txt = "color"..do...else do ...

Posted: Tue Nov 18, 2008 1:11 am
by requinix
strpos returns a number, at least zero, if it found the string; false if it didn't. You should be using === or !== to test the result.

What confuses me is why you changed it to !=true. That will pass if the word was the first on the line or if it wasn't found.
So either the word is first or your "if ___ appears do this piece of code else do this piece" is backwards.
But I wouldn't think it's the first because "if the word ___ appears" isn't the first thing I would think of saying if the line was entirely that word. However getting my comments in that code backwards seems equally unlikely.

Doesn't matter really, as long as it all works.