shage Posted July 14, 2010 Share Posted July 14, 2010 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 More sharing options...
kenrbnsn Posted July 14, 2010 Share Posted July 14, 2010 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 More sharing options...
shage Posted July 14, 2010 Author Share Posted July 14, 2010 I love you i love you i love you Link to comment https://forums.phpfreaks.com/topic/207732-random/#findComment-1085942 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.