Jump to content

[SOLVED] Showing empty line


Bricktop

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/166971-solved-showing-empty-line/
Share on other sites

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

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.