我对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??)
非常感谢任何想法。
答案 0 :(得分:2)
您提到的代码会在每次迭代时覆盖$ date变量的内容,因此当您运行$file = file_get_contents('idfilebuy'.$datef.'.txt');
$ datedef on包含最后一次迭代时。
您需要在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);