Page 1 of 1

Read the content of the file randomly

Posted: Sat Oct 27, 2007 8:39 am
by asif_phpdn
How can I read the content of the file(i.e,the sentences of a text file) randomly?
:?

Posted: Sat Oct 27, 2007 8:56 am
by seppo0010
Weird question... does this help you?

Code: Select all

$text = file_get_contents('my_text_file.txt');
$sentences = explode('.', $text);
echo trim($sentences[array_rand($sentences)]);

Posted: Sat Oct 27, 2007 1:21 pm
by s.dot
It'd be best if you could get each sentence on a new line. If you did so, the following would be much better than searching for a sentence (periods may not delimit a sentence).

Code: Select all

<?php
$sentences = explode("\n", file_get_contents('file.txt'));
shuffle($sentences);

echo '<pre>';
print_r($sentences);
echo '</pre>';

Posted: Sat Oct 27, 2007 1:22 pm
by feyd
file() may do well too. ;)