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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

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

Post 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 !
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

Code: Select all

$file = file("setting.txt");
if (strpos($file[4], "colored") !== false) {
    // this piece of code
} else {
    // this piece
}
User avatar
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 ...

Post 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" ?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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
}
User avatar
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 ...

Post by Peuplarchie »

why do you say it confused you ?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
Post Reply