Jump to content

Random


shage

Recommended Posts

function RandomLine($file, $amount) {
$filename = ''.$file.'';
$how_many_to_show = $amount; 
if ($fileContents = file($filename)) { 
shuffle($fileContents); 
for ($i = 0; $i < $how_many_to_show; $i++) { 
    $here12 = str_replace("\n", "", $fileContents[$i]);    
$damn = $how_many_to_show-1;
if ($i == $damn)
  print $here12;
else
  print $here12.', ';
}

}
}

 

 

Im trying to have it say random 5 lines from a text file to create say a paragraph, then submit it to a form, any idea why i cant do it with the current code scratch heads

Link to comment
https://forums.phpfreaks.com/topic/207732-random/
Share on other sites

You keep on overwritting $here2 with this statement

<?php
$here12 = str_replace("\n", "", $fileContents[$i]); 
?>

 

Here's your code rewritten:

<?php
function RandomLine($file, $amount) {
$para = '';
if ($fileContents = file($file)) {
	$fileContents = array_map('trim',$fileContents);
	shuffle($fileContents);
	$para = implode(' ',array_slice($fileContents,0,$amount));
}
return ($para);
}
?>

 

This should do what you want, but I haven't tested it or done a syntax check.

 

Ken

 

Link to comment
https://forums.phpfreaks.com/topic/207732-random/#findComment-1085938
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.