Jump to content

[SOLVED] Text file null


johnny-2step

Recommended Posts

Hi all,

 

I have a problem concerning outputting a string from a text file.  Here's a sample of my code:

 

$handle = fopen("country.txt", "r");

while(!feof($handle))
{
   $location = fgets($handle);
   if(is_null($location))
   {
      echo "UNKNOWN";
   }
   else
   {
      echo $location;
   }
}

 

This is what my text file kinda looks like:

 

location1

location2

<a null space>

location3

 

Now my problem is, instead of my code outputting an "UNKNOWN" text whenever it encounters the null space it just shows a blank.  I don't know what I'm doing wrong.  I tried empty(), !isset() and $location == "" on my condition statement but the result is still the same.  I'm probably missing something but I just can't figure it out.  Any help would be really appreciated.  Thanks in advance.

Link to comment
Share on other sites

Hi teng84,

 

Sorry, if I wasn't able to make it clear.  My first time using php you see.  Well, I'm trying to get my script to get data from a text file line by line and show it.  When my script encounters a blank line within the text then it will output an "UNKNOWN" message.  But what my script is doing is rather show an "UNKNOWN" text, it still goes through in showing the blank line.  I have tried all kinds of condition statements just so to get it to work but to no avail.  I hope you get what I'm trying to accomplish.

 

If you can suggest a different approach, I would gladly like to hear about it.

Link to comment
Share on other sites

$handle = fopen("test.php", "r");

 

while(!feof($handle))

{

  $location = fgets($handle);

  if($location=='')

  {

      echo "UNKNOWN";

  }

  else

  {

      echo $location;

  }

}

 

this works fine on me

Link to comment
Share on other sites

Hey guys,

 

Thanks for all your help.  Finally got it to work.  I just needed to use the trim() function to get it not to pass on my if condition statement.  Thanks pocobueno1388.  I nearly had the urge to hit my head on the keyboard just trying to figure this out.  Again thanks.

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.