473,378 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Quote of the day...

Hi I'm a newbie. I want to create a simple script that reads a text
file (quotes.txt) and picks a quote from the file at random. There will
be one quote per line in the file. Any help would be greatly
appreciated.

Aug 18 '05 #1
6 2066
On 18 Aug 2005 14:54:10 -0700, go****@sigol.co.uk wrote:
Hi I'm a newbie. I want to create a simple script that reads a text
file (quotes.txt) and picks a quote from the file at random. There will
be one quote per line in the file. Any help would be greatly
appreciated.


A näive but simple and effective for smallish files could just use:

http://uk2.php.net/file
http://uk.php.net/array_rand

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Aug 18 '05 #2
On 18 Aug 2005 14:54:10 -0700, go****@sigol.co.uk wrote:
Hi I'm a newbie. I want to create a simple script that reads a text
file (quotes.txt) and picks a quote from the file at random. There will
be one quote per line in the file. Any help would be greatly
appreciated.


A näive but simple and effective approach for smallish files could just use:

http://uk2.php.net/file
http://uk.php.net/array_rand

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Aug 18 '05 #3
go****@sigol.co.uk wrote:
Hi I'm a newbie. I want to create a simple script that reads a text
file (quotes.txt) and picks a quote from the file at random. There will
be one quote per line in the file. Any help would be greatly
appreciated.


Very easy.

$Quotes = file("quotes.txt"); // read the file quotes.txt and put all
lines into the array $Quotes
$Quote = $Quotes[array_rand($Quotes)]; // choose a random line from the
array $Quotes and put it in the string $Quotes
$Quote = rtrim($Quote); // strip 'End of Line' character at the end of
the quote
Aug 19 '05 #4
<comp.lang.php , , go****@sigol.co.uk>
<11**********************@g14g2000cwa.googlegroups .com>
<18 Aug 2005 14:54:10 -0700>
Hi I'm a newbie. I want to create a simple script that reads a text
file (quotes.txt) and picks a quote from the file at random. There will
be one quote per line in the file. Any help would be greatly
appreciated.


I have my random quote stuff currently in the config file and i've been
meaning to convert it to a text file .

Its your lucky day as I had to write the code anyway .

phpgb_rnx_random.php can be any text file you want and the file
extension used doesnt matter .
<?php

$phpgb="phpgb_rnx_random.php";
$filename=$phpgb;
$fp=fopen($filename,"r");

$toke=0;

while (!feof($fp))
{
$jack[$toke]=fgets($fp);
$toke=$toke+1;
}

$toke=$toke-2;
fclose($fp);

$zzz=(rand()%$toke);

print "There are $toke random lines <br>";
print "Random Number = $zzz <br>";
print "$jack[$zzz] <br>";

?>
--
www.phpguestbook.co.uk
Aug 20 '05 #5
<comp.lang.php , PHPGB , we*******@phpgb.co.uk.INVALID>
<MP************************@no-cancel.newsreader.com>
<Sat, 20 Aug 2005 13:39:08 +0100>
<?php
...
?>


www.phpguestbook.co.uk/phpgb_rnd_random.php

To see it working .
Aug 20 '05 #6
To everybody - thank you very much for your help. It's much
appreciated.
Regards,
Sigol.

Aug 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: jmar | last post by:
I posted on this topic a while back and received some good responses. However, I have better insight into what I'm looking to do so I am tapping the wealth of experience here again hoping to find a...
3
by: nyenyec | last post by:
urllib.quote chokes on unicode in 2.4.4. 2.4.4 (#1, Oct 18 2006, 10:34:39) Traceback (most recent call last): File "<stdin>", line 1, in ? File...
4
by: wesbland | last post by:
>From my understanding, when a string is stored in VB.NET and you look at it in the debugger, it has a quote on both sides to signify that it is a string as opposed to a char or int or whatever. ...
8
by: plemon | last post by:
alright here is what it is: a computer site where customers build custom system by selecting from forms and radio buttons. what is needed: a database (MySQL) with php basicly... 1 users...
4
by: mtugnoli | last post by:
This is my XML file <?xml version="1.0" encoding="utf-8"?> <index> <folder Name="Test1"> <folder Name="Test2"> <files> <file Name="After You've Gone.mp3"/> <file Name="Prova.mp3"/> </files>
10
by: =?Utf-8?B?Qm9iQWNoZ2lsbA==?= | last post by:
How can I use a quote as a literal so it does get confused as not a literal? Thanks! Bob
3
by: Rajesh | last post by:
Have you guys wanted to have stock tickers on ur desktop/ web pages, but can't use applets as they were heavy, too much configuration than here is one approach and light weight solution all for...
4
by: wxPythoner | last post by:
There's a thing that bugs me in Python. Look at this... SyntaxError: EOL while scanning single-quoted string Please focus on the part of the error message that states "while scanning...
3
by: rajmohan.h | last post by:
Hi all, Suppose I have a string which contains quotes inside quotes - single and double quotes interchangeably - s = "a1' b1 " c1' d1 ' c2" b2 'a2" I need to start at b1 and end at b2 - i.e. I...
9
by: WebArchitect | last post by:
I have a text file. My text file contains lines with double quotes around it. I trying to code if a line has double quotes around it Then make it bold My code is below and it is not working....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.