最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php 文件中随机取出一条数据四种方法
时间:2022-06-24 23:08:58 编辑:袖梨 来源:一聚教程网
| 代码如下 | 复制代码 |
|
//第一种方法: $line = getrandline1('test.txt'); function getrandline1($filename) { $linenum = 0; $fh = fopen($filename, 'r'); while(!feof($fh)) { if($rowcontents = fgets($fh)) { $linenum++; $contens[] = $rowcontents; } } $randline = mt_rand(0, $linenum - 1); $line = $contens[$randline]; fclose($fh); return $line; } //第二种方法: $line = getrandline2('test.txt'); function getrandline2($filename) { $contents = file('test.txt'); $linenum = count($contents); $randline = mt_rand(0, $linenum - 1); $line = $contents[$randline]; return $line; } //第三种方法: $line = getrandline3('test.txt'); function getrandline3($filename) { $contents = file('test.txt'); shuffle($contents); return $contents[0]; } //第四种方法: $line = getrandline4('test.txt'); function getrandline4($filename) { $linenum = 0; $fh = fopen($filename, 'r'); while(!feof($fh)) { if($linecontents = fgets($fh)) { $linenum++; $randint = (mt_rand(1, 1000000 * $linenum) - 1)/1000000); if($randint < 1) { $line = $linecontents; } } } fclose($fh); return $line; } ?> |
|
相关文章
- dnf神界版本恩特精灵圣杯完美词条属性搭配攻略 11-21
- 死神vs火影3.3隐藏技能表 死神vs火影3.3隐藏技能大全 11-21
- 三国天下归心许诸单点爆杀流攻略 11-21
- 博德之门3带电马甲获取方法 带电马甲在哪 11-21
- 波斯王子失落的王冠神秘宝箱全收集攻略 匠师支线任务攻略 11-21
- 圣兽之王全人物加入条件 圣兽之王全人物加入条件攻略介绍(图) 11-21