php怎么调用随机调用多个txt某一部分内容?或者调用文件下的随机txt里面N行数据?

<?php
$arr=file("1.txt");//请使用准确的文件名代替txt
$n=count($arr)-1;
for ($i=1;$i<=10;$i++){//100的需要显示的行数的例子,不是100时请换为具体数
$x=rand(0,$n);
echo $arr[$x],"\n","<br />";//随机显示一行
}
?>
这个是调用一个txt,怎么改成多个txt或者文件下面的所有txt内容随机

要调用随机调用多个txt的某一部分内容,您可以使用 PHP 中的 fopen 函数打开每个文件,然后逐行读取每个文件的内容,将需要的内容存储在一个数组或变量中。
以下是一个示例代码,它将随机调用多个 txt 文件中的某一部分内容:
<?php
// 定义要调用的 txt 文件的路径和名称
$files = array("file1.txt", "file2.txt", "file3.txt");
// 定义要调用的内容的起始和结束行号
$start = rand(0, count($files) - 1);
$end = $start + rand(5, 10);
// 定义存储调用内容的数组
$contents = array();
// 循环读取每个 txt 文件的内容并存储到数组中
foreach ($files as $file) {

if (file_exists($file)) {

$handle = fopen($file, "r");

while (!feof($handle)) {

$line = fgets($handle);

if ($line && $line{0} != '#') {

$contents[] = $line;

}
}

fclose($handle);

}
}
// 输出调用的内容
echo implode("", $contents);?>

要调用文件下的随机 txt 中的 N 行数据,您可以修改上述代码,将每个文件的起始行号设置为 N+1,并在循环中使用 fgets 读取 N 行数据。以下是一个示例代码:
<?php
// 定义要调用的 txt 文件的路径和名称
$files = array("file1.txt", "file2.txt", "file3.txt");
// 定义要调用的内容的起始和结束行号
$start = rand(0, count($files) - 1);
$end = $start + rand(5, 10);
// 定义存储调用内容的数组
$contents = array();
// 循环读取每个 txt 文件的内容并存储到数组中
foreach ($files as $file) {

if (file_exists($file)) {

$handle = fopen($file, "r");

while (!feof($handle)) {

$line = fgets($handle);

if ($line && $line{0} != '#') {

$contents[] = $line;

if ($line == $end) {

break;

}
}
}

fclose($handle); }
}
// 输出调用的内容
echo implode("", $contents);?>
温馨提示:答案为网友推荐,仅供参考
第1个回答  2023-05-19
要调用多个txt文件或文件夹下的所有txt文件内容,并进行随机处理,你可以使用递归来实现。下面是一个示例代码:
```php
<?php
function getRandomLinesFromFiles($directory, $numLines) {
$filePaths = glob($directory . '/*.txt'); // 获取文件夹下所有txt文件的路径

$lines = array();
foreach ($filePaths as $filePath) {
$arr = file($filePath);
$n = count($arr) - 1;

for ($i = 0; $i < $numLines; $i++) {
$randomLine = $arr[rand(0, $n)];
$lines[] = $randomLine;
}
}

return $lines;
}
$directory = 'path/to/directory'; // 替换为实际的文件夹路径
$numLines = 10; // 替换为需要获取的行数
$randomLines = getRandomLinesFromFiles($directory, $numLines);
foreach ($randomLines as $line) {
echo $line . "<br />";
}
?>
```
在上面的代码中,`getRandomLinesFromFiles`函数接受一个文件夹路径和需要获取的行数作为参数。它首先使用`glob`函数获取文件夹下所有txt文件的路径,然后遍历每个文件,随机选择指定数量的行,并将它们存储在一个数组中。最后,它返回包含随机行的数组,你可以根据需要进行进一步处理。
请注意,你需要将`$directory`变量替换为实际的文件夹路径,并根据需要修改`$numLines`变量来设置需要获取的行数。
第2个回答  2023-04-29

核心是俩函数:glob和shuffle
glob是扫目录下符合要求的文件,shuffle是打乱数组。

demo见下,写了注释,应该能看懂

<?php

$dir = "txt_files/"; // 指定 txt 文件所在文件夹路径

$files = glob($dir . "*.txt"); // 获取文件夹下所有 txt 文件的路径

$lines = array(); // 存储所有 txt 文件中的行数据

foreach ($files as $file) {

$content = file($file); // 读取 txt 文件中的所有行

$lines = array_merge($lines, $content); // 将该文件中的行合并到 $lines 数组中

}

shuffle($lines); // 将 $lines 数组中的元素随机排序

$selectedLines = array_slice($lines, 0, 10); // 选取随机的 10 行数据

foreach ($selectedLines as $line) {

echo $line, "<br>"; // 输出每行数据

}

?>

第3个回答  2023-05-08
$arr=file("1.txt");//请使用准确的文件名代替txt
修改为:
$arr = array_merge(file("1.txt"), file("2.txt"), file("3.txt"));
其它语句不变本回答被网友采纳
第4个回答  2023-05-08
php的精华就是少写废话
<?php
function showTxt($rand) {
$txt = file_get_contents("./$rand.txt");
$lines = explode("\n",$txt);
return $lines[mt_rand(0, count($lines))];
}
echo $showTxt("1.txt");
//或是把上面这行换成下面两行,随机读写.
$fileList = ["1.txt", "2.txt",'fuck.txt'];
echo $showTxt($fileList[mt_rand(0, count($fileList))]);
//如果是纯数字命名更简单
echo $showTxt(mt_rand(0,文件数) . ".txt");
相似回答
大家正在搜