How to get Random Lines of A Text File using php?

Find best Web Development tips and Tricks.
Post Reply
arnlweb
Site Admin
Posts: 95
Joined: Wed Jun 22, 2016 1:28 pm
Location: India
Contact:

How to get Random Lines of A Text File using php?

Post by arnlweb » Sun Jan 29, 2017 11:59 am

Here is the example... you can get Random Lines like this.

Code: Select all

<?php	
function RandomLine() {
	$textfile = "index/bollywoodmovies.txt";
		if(file_exists($textfile)){ 
			$sites =file($textfile); 
			$string = $sites[array_rand($sites)];
		} else {
			$string = "Error";
		}
	return $string;
}
	
	$s0 = RandomLine(rand((float) microtime(), 10000000));
	$s1 = RandomLine(rand((float) microtime(), 10000000));
	$s2 = RandomLine(rand((float) microtime(), 10000000));
	$s3 = RandomLine(rand((float) microtime(), 10000000));
	$s4 = RandomLine(rand((float) microtime(), 10000000));
	$s5 = RandomLine(rand((float) microtime(), 10000000));
	$s6 = RandomLine(rand((float) microtime(), 10000000));
	$s7 = RandomLine(rand((float) microtime(), 10000000));
	$s8 = RandomLine(rand((float) microtime(), 10000000));
	$s9 = RandomLine(rand((float) microtime(), 10000000));
			 	
?>

		<li><a href="<?php echo site_url().'/'.permalink_search().'/'.permalink($s0). $permalink_structure; ?>"><?php echo $s0; ?></a></li>
		<li><a href="<?php echo site_url().'/'.permalink_search().'/'.permalink($s1). $permalink_structure; ?>"><?php echo $s1; ?></a></li>
		<li><a href="<?php echo site_url().'/'.permalink_search().'/'.permalink($s2). $permalink_structure; ?>"><?php echo $s2; ?></a></li>
		<li><a href="<?php echo site_url().'/'.permalink_search().'/'.permalink($s3). $permalink_structure; ?>"><?php echo $s3; ?></a></li>
		<li><a href="<?php echo site_url().'/'.permalink_search().'/'.permalink($s4). $permalink_structure; ?>"><?php echo $s4; ?></a></li>
		<li><a href="<?php echo site_url().'/'.permalink_search().'/'.permalink($s5). $permalink_structure; ?>"><?php echo $s5; ?></a></li>
		<li><a href="<?php echo site_url().'/'.permalink_search().'/'.permalink($s6). $permalink_structure; ?>"><?php echo $s6; ?></a></li>
		<li><a href="<?php echo site_url().'/'.permalink_search().'/'.permalink($s7). $permalink_structure; ?>"><?php echo $s7; ?></a></li>
		<li><a href="<?php echo site_url().'/'.permalink_search().'/'.permalink($s8). $permalink_structure; ?>"><?php echo $s8; ?></a></li>
		<li><a href="<?php echo site_url().'/'.permalink_search().'/'.permalink($s9). $permalink_structure; ?>"><?php echo $s9; ?></a></li>
Hope it help you. :D :) :D

Post Reply