LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-08-2004, 12:43 PM   #1
Erik Thorsson
Member
 
Registered: Jun 2004
Location: NYC
Distribution: Fedora Core 1
Posts: 64

Rep: Reputation: 15
PHP - read a random line from a file?


Is there a function in PHP that can read a random line from a file into a variable?

I am trying to write a dynamic biography where the name, dates, and locations change each time you load it.

Right now I am holding the dynamic bits in text files. Or is this an application mysql would be better suited for, ultimately?

-E
 
Old 12-08-2004, 01:33 PM   #2
Erik Thorsson
Member
 
Registered: Jun 2004
Location: NYC
Distribution: Fedora Core 1
Posts: 64

Original Poster
Rep: Reputation: 15
Nevermind. Found it. From: http://jerrett.liquidpulse.net/code/...on/random_line

Random Line
Using this you can call the same function multiple times on diffrent pages, or on the same page - and call diffrent files each time.

RandomLine("quotes.txt");


<?

function RandomLine($filename) {
$lines = file($filename) ;
echo $lines[array_rand($lines)] ;
}
?>
 
Old 12-10-2004, 08:24 AM   #3
Erik Thorsson
Member
 
Registered: Jun 2004
Location: NYC
Distribution: Fedora Core 1
Posts: 64

Original Poster
Rep: Reputation: 15
Okay, now I've found that code, but I find myself wanting to put the random string in a variable to be used later in the text (e.g. choosing a random name at the beginning and referring to it later).

$firstname = RandomLine("bio/firstname");

...doesn't do it though. The variable ends up empty, and the line from the 'firstname' file gets printed.

Why? And how can I get the value of the string output by the RandomLine function into a variable?

-E
 
Old 12-10-2004, 10:31 AM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
That is simple...
PHP Code:
function RandomLine($filename) {
    
$lines file($filename) ;
    return 
$lines[array_rand($lines)] ;
}
$firstname RandomLine("bio/firstname");
echo 
$firstname
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
Bash - choose a random line from a file ericcarlson Linux - Software 27 11-12-2013 01:25 AM
bash: read a line from file zokik Linux - General 6 12-10-2008 09:24 AM
C: fread to read a file line by line until the end Blue_muppet Programming 2 09-19-2008 09:42 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration