Home > Article > Backend Development > PHP prints random lines of text_PHP tutorial
This code can print a random number of lines in the text. For example, there are 1W lines in a text and I need to randomly take out 500 lines.
1. $file="txt";
2. $data=file_get_contents($file);
3. $arr=explode("n",$data);
4. $count=count($arr)-1;
5. $num=500;
6. for($i=1;$i<=$num;$i++){
7. $n=rand(0,$count);
8. echo $arr[$n]."n";
9. }
This article comes from the "mcshell learning blog" blog