Stucturing PHP for line breaks

Hi,

I have practically no idea how to get the following done.

I have a .txt file with data in the format below:

demagogue : a leader who makes use of popular prejudices and false claims and promises in order to gain power |
bucket shop : a dishonest brokerage firm |
collywobbles : pain in the abdomen and especially in the stomach |

And this my php :

<?php $file = fopen("interesting_words.txt","r"); $raw_string = fread($file,filesize("interesting_words.txt")); fclose($file); $words_array = explode("|",$raw_string); echo $words_array[array_rand($words_array)]; srand(date("ymd")); $wordIndex = rand(0, $wordCount); ?>

I would like this to appear as follows when rendered in a box:

                                                     BUCKET SHOP

                                           a dishonest brokerage firm

This is for a WORD OF THE DAY page for my language students.

Any help will be much appreciated.

This little bit of code should do what you want without the |
I’ve tried to comment it to let you know what’s going on
[php]
//Check your text file exits
if (file_exists(‘interesting_words.txt’)) {
//Retrieve content of text file
$content = file_get_contents(‘interesting_words.txt’, true);
//Add every line of file to array
$lines = explode("\n", $content);

//Cycle through all line in file
$words = array();
foreach ($lines as $line) {
	//Split word and description by :
    list($word, $description) = explode(':', $line);
    //Create a words array to hold word and description
    $words[] = array('word' => ucfirst(trim($word)), 'description' => ucfirst(trim($description)));
}
//Generate random number from date
//Remove to generate random word every page load
srand(date("ymd"));
//Use random number from 0 to the cound of words array to pick word of the day
$word_of_the_day = $words[rand(0, (count($words) -1))];

//Use word and description keys to print the results.
echo $word_of_the_day['word'] . '<br>';
echo $word_of_the_day['description'] . '<br>';

}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service