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 Code:
<?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 Code:
<?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);
?>
Bookmarks