ryandoss Posted July 30, 2012 Share Posted July 30, 2012 I feel ridiculous for having to ask this but I just can't figure out what the issue is! The if statement below simply does not EVER return true. $lines = file('file.txt'); foreach($lines as $line) { if($line == "--") { // Do Work } } The text file as follows: Lothian 343,463 259,548 265,561 -- Mahaw 539,32 480,36 451,42 -- Rheged 413,724 504,762 495,772 I've changed the "--" to several other characters like a empty line or the word "new" and even :: or || What am I missing here? lol Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 30, 2012 Share Posted July 30, 2012 try adding $line = trim($line); before the if, and if that does't help do a vardump on $line and see what each line contains. I think it might contain the new line character, and trim should remove it. Quote Link to comment Share on other sites More sharing options...
ryandoss Posted July 30, 2012 Author Share Posted July 30, 2012 Ahhh yes of course!.. I tried "--\n" but I guess it doesn't work like that lol! Thanks so much : ) Quote Link to comment Share on other sites More sharing options...
Christian F. Posted July 30, 2012 Share Posted July 30, 2012 The reason "--\n" didn't work is most likely because the text file in question was made by a Windows application. Since Windows uses "\r\n" for newlines, well... I don't think I need to explain further. That's why you'll always want to use trim () on content you get from file (), unless it's important that you preserve newlines exactly as they are. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.