如何从文本文件中回显随机行 [英] How to echo random line from text file

查看:94
本文介绍了如何从文本文件中回显随机行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文本文件格式是:

My text file format is :


这是第一行。
这是第二行。
这是第三行。

This is first line.
This is second line.
This is third line.

文本文件中可能有更多行。如何在PHP的文本文件刷新每个刷新随机行。
所有意见都表示赞赏。谢谢

There could be more lines in the text file. How to echo one random line on each refresh from the text file with php. All comments are appreciated. Thanks

推荐答案

这种情况的一个好的方法是:

A generally good approach to this kind of situation is to:


  1. 使用 c $ c $> c $ c $> c $ c $>> 一个href =http://us3.php.net/manual/en/function.array-rand.php =noreferrer> array_rand()

  1. Read the lines into an array using file()
  2. echo out a random array value using array_rand()



您的代码可能如下所示:


Your code could look something like this:

$lines = file('my_file.txt');

echo $lines[array_rand($lines)];

这篇关于如何从文本文件中回显随机行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆