Bricktop Posted July 22, 2009 Share Posted July 22, 2009 Hi chaps, I'm nearly done with my little project, sorry for all the posts but thanks for your time. Anyway, I have the following code which reads in a file, then outputs a random line from that file. But, how do I stop it from outputting a blank output if the line it selects is blank? <?php $quotesfile = 'text.txt'; //Open the quotes file $readcontent = fopen($quotesfile, "r"); //Read the contents of the quotes file and store in the $quotescontent variable $quotescontent = fread($readcontent, filesize($quotesfile)); //Close the quotes file fclose($readcontent); //Check if the quotes file contains quotes and if so continue with below if( $quotescontent != "" ) { //Split the quotes and store in the $quotes variable $quotes = explode("\n", $quotescontent); //Count the number of quotes $number = count($quotes); //Generate a random quote using PHP's "rand" function $quote = rand(0, $number - 1); //Divide the quote and the author of the quote (separated by the || in the quotes.txt file) $displayquote = explode("||", $quotes[$quote]); //Display the quote and the author $content .= '<blockquote class="quotetext">'.$displayquote[0].'</blockquote><br /><br />'; $content .= '<cite class="authortext">'.$displayquote[1].'</cite>'; echo $content; } //If the quotes file is empty, display the below else { $content .= '<span class="quotetext">There are no quotes to show.</span>'; echo $content; } ?> I tried adding if($displayquote[0] != "") just above the output code but that didn't have much effect as when it came across a blank line the script just stopped. I suppose what I need it to do if it comes across a blank line is to loop through the display code again and choose another random line. Is that possible? Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/166971-solved-showing-empty-line/ Share on other sites More sharing options...
.josh Posted July 22, 2009 Share Posted July 22, 2009 if (trim($quotescontent) != "") Quote Link to comment https://forums.phpfreaks.com/topic/166971-solved-showing-empty-line/#findComment-880327 Share on other sites More sharing options...
Bricktop Posted July 22, 2009 Author Share Posted July 22, 2009 Thanks for this Crayon Violent but it doesn't seem to work, I've tried: if (trim($quotescontent) != "") and I've also tried trim($quotes) and trim($dispayquote) but it just doesn't seem to be trimming the blank lines from the output. Any other ideas? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/166971-solved-showing-empty-line/#findComment-880340 Share on other sites More sharing options...
Bricktop Posted July 22, 2009 Author Share Posted July 22, 2009 Scrap that, got it going now with: $quotescontent = trim($quotescontent); Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/166971-solved-showing-empty-line/#findComment-880343 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.