Jump to content

Fetching random lines beginning with a specific word


rachelk

Recommended Posts

Hi,

 

I was wondering if there was a way to fetch a random line from a txt file that started with a specific word... the txt file looks something like this:

 

word1 more words here

word1 more words here

word2 more words here

word2 more words here

word2 more words here

word3 more words here

word3 more words here

word4 more words here

 

And I was hoping for a way to fetch a random line only from those beginning with "word2" for example.

 

Any help is much appreciated!

 

Thanks,

Rachel

pretty easy with mysql, but with a flat text file I can think of two ways:

 

1. Line by line, dumping each line that starts with your target word into an array, then randomizing and display one value.

2. Sucking the entire file into a variable, then doing a preg_match_all for your first word. Then, you can randomize that array and display one value.

 

http://php.net/fgets

http://php.net/array_rand

 

http://php.net/file_get_contents

http://php.net/preg_match_all

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.