当前位置:编程学习 > php >>

php打印文本中随机行内容

 

此段代码可以打印文本中的随机行数,例如,一个文本中有1W行,我需要随机取出500行。

 

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.    }

  

 

 

本文出自 “mcshell学习博客” 博客

补充:Web开发 , php ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,