rachelk Posted May 11, 2009 Share Posted May 11, 2009 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 Link to comment https://forums.phpfreaks.com/topic/157645-fetching-random-lines-beginning-with-a-specific-word/ Share on other sites More sharing options...
michaellunsford Posted May 11, 2009 Share Posted May 11, 2009 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 Link to comment https://forums.phpfreaks.com/topic/157645-fetching-random-lines-beginning-with-a-specific-word/#findComment-831296 Share on other sites More sharing options...
rachelk Posted May 11, 2009 Author Share Posted May 11, 2009 Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/157645-fetching-random-lines-beginning-with-a-specific-word/#findComment-832040 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.