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 !
If, line 5 of file test.txt = "color"..do...else do ...
Moderator: General Moderators
- Peuplarchie
- Forum Contributor
- Posts: 148
- Joined: Sat Feb 04, 2006 10:49 pm
Re: If, line 5 of file test.txt = "color"..do...else do ...
Code: Select all
$file = file("setting.txt");
if (strpos($file[4], "colored") !== false) {
// this piece of code
} else {
// this piece
}- Peuplarchie
- Forum Contributor
- Posts: 148
- Joined: Sat Feb 04, 2006 10:49 pm
Re: If, line 5 of file test.txt = "color"..do...else do ...
Nice, small and sirect to the point..
I had to change the
to
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" ?
I had to change the
Code: Select all
!== false
Code: Select all
!= true
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 ...
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
}- Peuplarchie
- Forum Contributor
- Posts: 148
- Joined: Sat Feb 04, 2006 10:49 pm
Re: If, line 5 of file test.txt = "color"..do...else do ...
why do you say it confused you ?
Re: If, line 5 of file test.txt = "color"..do...else do ...
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.
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.