有关该功能的信息:目前它抓取最后20行,并根据行中请求的列显示信息。
我想做什么
我想忽略包含相同列5和13的行。 示例:(应忽略下面的两行,因为第一行中的第5列与第13行匹配,第二行与第一行相同,因为第5列中的第一个名称与第13列的名称相匹配)
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
因此,脚本应该抓住额外的行并忽略列匹配的行,直到它可以显示20行有效行。
以下是我的实际功能:
function DMMRankings()
{
# read a file into an array
$lines = file('C:/path/to/file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
# take the last 20 lines of the file -- i.e. the last 20 values in the array
$last_ten = array_slice($lines, -20);
#create an array for the output
$n = 1;
$content = '';
foreach ($last_ten 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];
}
# store the data we want in an output array.
$data = array('rank-pos' => $n++, 'rank-name' => $arr[4], 'rank-dmuser' => $arr[12]);
$content .= Template::Load('rankinguserdm-' . ($n % 2 == 1 ? 2 : 1), $data);
}
$this->content = Template::Load('user_rankingsdm', array('rankings' => $content));
}
请帮我解决一下我目前的功能如何做到这一点。谢谢!
答案 0 :(得分:0)
这里有一些代码可以测试col 5和col 13是否相同,如果是,则跳过该行。它使用累加器来计算有效行数。请参阅代码以获取有关它正在做什么的评论。
# 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 = 0;
$wanted = 20; # 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;
}
}
# check if we got enough data from the loop
if ($n < $wanted) {
die("We couldn't get enough data!");
}
答案 1 :(得分:0)
要获得20个有效行,只需保持循环直到你有20个。
$lastline=count($lines);
$dsp=0;
while(dsp<20){
//check to see if there are any more lines
if(!$lines[$lastline-$dsp]){
echo 'oops, we ran out of lines';
break;}
$l=$lines[$lastline-$dsp];
# 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];
}
//skip if 5==13
if($arr[4]==$arr[12])continue;
//else: add one to counter
$dsp++;
# store the data we want in an output array.
$data = array('rank-pos' => $n++, 'rank-name' => $arr[4], 'rank-dmuser' => $arr[12]);
$content .= Template::Load('rankinguserdm-' . ($n % 2 == 1 ? 2 : 1), $data);
}