我只有一个选项可以从这个文本文件中获取数据,因此我无法使用数据库来存储它。每天00:00重新创建函数抓取数据的文件,因此它不会成为一个非常大的问题。最大2 MB的大小,一天结束时最多6,000 - 7,000行。 我担心的是它会抓取数据并将其显示在可以被访问很多次的网页上(approximately up to 10,000 per day or less
) - 它是否真的会使用file()
或者这个小的服务器使服务器过载文件应该没问题?请告诉我。感谢您抽出宝贵时间阅读我的问题,并可能回答。
.txt文件中的示例行:
1,42,16, 201,stackoverflow_user, 1, 6762160, 39799, 9817242, 6762160, 39884, 10010545,stackoverflow_user, 2, 1351147, 1165, 483259, 1351147, 1115, 241630, 0
1,46,27, 201,[stackoverflow_user | stackoverflow_userother], 1, 4078465, 286991, 1594830, 4078465, 287036, 1643156,stackoverflow_user, 2, 1357147, 1115, 241630, 1357147, 1065, 120815, 0
我的功能:
# read a file into an array
$lines = file('C:/path/to/file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
# flip our array over so the last lines of the file are first.
$lines = array_reverse($lines);
$n = 1;
$wanted = 21; # or however many lines you want.
$content = '';
foreach ($lines as $l) {
# treat the data as comma-separated values
$arr = explode(",", $l);
# if col 5 has multiple values, take the first one
if (preg_match("/\[(.+?) \|/", $arr[4], $matches)) {
$arr[4] = $matches[1];
}
# is arr[4] the same as arr[12]?
if ($arr[4] !== $arr[12]) {
# these two are not equal, so use these values
$data = array('rank-pos' => $n++, 'rank-name' => $arr[4], 'rank-dmuser' => $arr[12]);
$content .= Template::Load('rankinguserdm-' . ($n % 2 == 1 ? 2 : 1), $data);
}
# have we got enough data?
if ($n === $wanted) {
break;
}
}
$this->content = Template::Load('user_rankingsdm', array('rankings' => $content));
}