Jump to content

PHP file function


ryandoss

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/266454-php-file-function/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/266454-php-file-function/#findComment-1365497
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.