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
Share on other sites

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.

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.