You are here: Re: Select a random line from a text file « All PHP « IT news, forums, messages
Re: Select a random line from a text file

Posted by MaKroZ on 09/27/28 11:41

On Sat, 04 Mar 2006 10:45:13 -0800, guttyguppy wrote:

> I know that file('mytext.txt'); will return an array of all the lines
> of text in mytext.txt. What I want is to grab the text from random line
> in mytext.txt.
>
> $lines = file('words.txt');
> $random = rand() % 100;
> echo "line=" . $lines[$random];
>
> The problem is I have a very long text file, and each time the php page
> is loaded there is a delay as it needs to assemble the enormous array.
>
> How can I tell the php page to locate the random line number in
> mytext.txt and only load that variable? Thanks for any advice.

Apparently it might be wise to calculate the line number first then read
the desired line only rather then the entire file and build an array from
it. Right, there is a question: what address that line begins at?
Nobody knows. So you'd have to have all addresses of all the lines of your
text file and have them all stored somewhere else in short form for fast
access. It's called indexing. Then you'll have to design and implement
your own set of functions to handle and process such kind of data. It
remind me another term: "database management systems". So why not to use
one of DBMSs, such as MySQL or PostgreSQL or any other nice DB?


--
nn My bookmarks:
(oo) http://www.yakutia.org
| | http://www.theisplist.com
|/\| http://www.tenpieces.com

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация