根据日期名称读取多个文本文件,并将所有文本文件写入以连接字符串


Read Multiple Text Files based on dated name and write all to concatenate string

我对PHP很陌生,本周一直在学习,但我被这个问题困住了。

我有多个文本文件,其名称基于日期。我需要读取日期范围内的每个文件,并将文本连接成一个长字符串。

到目前为止,我拥有的:

创建不同的日期作为字符串并写入变量$datef:

while (strtotime($date) <= strtotime($end_date)) {
$datef="$date'n";
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}

变量 $datef 用于动态文件名:

$file = file_get_contents('idfilebuy'.$datef.'.txt');
$string = ???? (all files to variable $string as concatenate string??)

任何想法将不胜感激。

您提到的代码在每次迭代时都会覆盖$date变量的内容,因此当您运行$file = file_get_contents('idfilebuy'.$datef.'.txt'); $datedef时,包含最后的迭代。

您需要检索 while 语句中的每个文件。

$string = '';
while (strtotime($date) <= strtotime($end_date)) {
    $datef="$date";
    $fileContent = file_get_contents('idfilebuy'.$datef.'.txt');
    $string .= $fileContent;
    $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
var_dump($string);