Log in

View Full Version : Resolved Fetching random lines beginning with a specific word



rachelk
05-11-2009, 01:26 AM
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

Nile
05-11-2009, 02:38 AM
Try something like this:


<?php
function randomString ($arr){
if(!is_array($arr)){
return '$arr not an array.';
}
if(count($arr) == 0){
return 'None found';
}
return $arr[rand(0, count($arr)-1)];
}
$file = fopen("words.txt", "rb");
$fetch = "word2";
$found = array();
while($line = fgetcsv($file, 100, " ")){
if($line[0] == $fetch){
$found[] = implode($line, " ");
}
}
echo randomString($found);
?>

rachelk
05-11-2009, 06:00 AM
Thanks so much for your help, it works perfectly! Would it also be possible to do this with a txt file that looked like this, with no spaces in between words?

/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

I tried changing
while($line = fgetcsv($file, 100, " ")){ to
while($line = fgetcsv($file, 100, "/")){ but it doesn't seem to work for me.

Nile
05-11-2009, 11:46 AM
This should do it:


<?php
function randomString ($arr){
if(!is_array($arr)){
return '$arr not an array.';
}
if(count($arr) == 0){
return 'None found';
}
return $arr[rand(0, count($arr)-1)];
}
$file = fopen("words.txt", "rb");
$fetch = "word2";
$found = array();
while($line = fgetcsv($file, 100, "/")){
if($line[1] == $fetch){
$found[] = implode($line, "/");
}
}
echo randomString($found);
?>

rachelk
05-11-2009, 11:34 PM
Thank you so much for your time Nile, I've just tested it out and it works perfectly! :)

I'm planning on having around 10 of these random lines on the same page (each one getting a random line with a different specific word at the start, from the same txt file), would it be ok to have 10 copies of this same piece of coding with the variable names changed for each? like

#1
<?php
function randomString ($arr){
if(!is_array($arr)){
return '$arr not an array.';
}
if(count($arr) == 0){
return 'None found';
}
return $arr[rand(0, count($arr)-1)];
}
$file = fopen("words.txt", "rb");
$fetch = "word2";
$found = array();
while($line = fgetcsv($file, 100, "/")){
if($line[1] == $fetch){
$found[] = implode($line, "/");
}
}
echo randomString($found);
?>

#2
<?php
function randomString2 ($arr2){
if(!is_array($arr2)){
return '$arr2 not an array.';
}
if(count($arr2) == 0){
return 'None found';
}
return $arr2[rand(0, count($arr2)-1)];
}
$file2 = fopen("words.txt", "rb");
$fetch2 = "word1";
$found2 = array();
while($line2 = fgetcsv($file2, 100, "/")){
if($line2[1] == $fetch2){
$found2[] = implode($line2, "/");
}
}
echo randomString2($found2);
?>

Nile
05-12-2009, 12:11 AM
I would do this:


<?php
function randomString ($arr){
if(!is_array($arr)){
return '$arr not an array.';
}
if(count($arr) == 0){
return 'None found';
}
return $arr[rand(0, count($arr)-1)];
}
function randomLine ($file, $start){
$file = fopen($file, "rb");
$found = array();
while($line = fgetcsv($file, 100, "/")){
if($line[1] == $start){
$found[] = implode($line, "/");
}
}
return randomString($found);
}
?>

Put that at the top of you're page, and then:
#1:


echo randomLine("words.txt", "word2");

#2:


echo randomLine("words.txt", "word1");


And just a little tip, never make a function that has the same actions with a different name, it's pointless.

rachelk
05-13-2009, 05:58 AM
Thanks so much Nile :)

Nile
05-13-2009, 11:47 AM
Anytime. ;)