LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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: 242Reputation: 242Reputation: 242
That is simple...
PHP Code:
function RandomLine($filename) {
    
$lines file($filename) ;
    return 
$lines[array_rand($lines)] ;
}
$firstname RandomLine("bio/firstname");
echo 
$firstname
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 - choose a random line from a file ericcarlson Linux - Software 27 11-12-2013 01:25 AM
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 08:16 PM
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


All times are GMT -5. The time now is 07:01 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration