我想从3个不同的txt文件中获取数据,然后随机显示数据

时间:2016-12-13 14:25:20

标签: php mysql text

现在我的系统从Mysql数据库中获取数据。现在我想从3个不同的txt文件中获取数据,然后随机显示数据。我在两个文本文件中都有多个行数据。我想使用PHP随机从两个文件中获取所有数据。然后想在两个数据源中进行分页。

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

您可以将文本文件行读入数组并随机选择一行:

<?php
$txt1 =<<<TXT
“If you tell the truth, you don't have to remember anything.” ― Mark Twain
“Good friends, good books, and a sleepy conscience: this is the ideal life.” ― Mark Twain
“Whenever you find yourself on the side of the majority, it is time to reform (or pause and reflect).” ― Mark Twain
TXT;

$txt2 =<<<TXT
“Facts do not cease to exist because they are ignored.” ― Aldous Huxley
“Words can be like X-rays if you use them properly -- they’ll go through anything. You read and you’re pierced.” ― Aldous Huxley
“After silence, that which comes nearest to expressing the inexpressible is music.” ― Aldous Huxley,
TXT;

$lines  = explode("\n", $txt1 . $txt2);
$random = array_rand($lines);

echo $lines[$random];

你可以改为:

$txt1 = file_get_contents('/path/to/text/file');

等等。