johnny-2step Posted July 5, 2007 Share Posted July 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 5, 2007 Share Posted July 5, 2007 i dont understand also y try to use "file" and array is better for me Quote Link to comment Share on other sites More sharing options...
johnny-2step Posted July 5, 2007 Author Share Posted July 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 5, 2007 Share Posted July 5, 2007 $handle = fopen("test.php", "r"); while(!feof($handle)) { $location = fgets($handle); if($location=='') { echo "UNKNOWN"; } else { echo $location; } } this works fine on me Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 5, 2007 Share Posted July 5, 2007 You can give this a shot as well: <?php $file = file("country.txt"); foreach ($file as $line){ $line = trim($line); if ($line != ""){ echo $line.'<br>'; } else { echo "UNKNOWN<br>"; } } ?> Quote Link to comment Share on other sites More sharing options...
johnny-2step Posted July 5, 2007 Author Share Posted July 5, 2007 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. 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.