Home >php教程 >php手册 >php打印文本中随机行内容

php打印文本中随机行内容

WBOY
WBOYOriginal
2016-06-13 10:45:351296browse

 

此段代码可以打印文本中的随机行数,例如,一个文本中有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

7.    $n=rand(0,$count);

8.    echo $arr[$n]."\n";

9.    }

  

 

 

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn