I want to do 2 things on my script below in which I am currently struggling.
Info about the function: Currently it grabs last 20 lines and display info based on the requested columns from the lines.
First) I want to ignore lines that contain same column 5 and 13. Example: (The both lines below should be ignored because column 5 in the first line matches column 13 and the second line is the same as the first one because first name from column 5 matches the name of the column 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
Second) I want to include reserved words. Similiar to the above but I should specify the reserved words: lets say if column 5 or 13 or both contain the name: STACK
, it should ignore these lines.
1,42,16, 201,STACK, 1, 6762160, 39799, 9817242, 6762160, 39884, 10010545,stackoverflow_usersecond, 2, 1351147, 1165, 483259, 1351147, 1115, 241630, 0
1,46,27, 201,[stackoverflow_user | stackoverflow_userother], 1, 4078465, 286991, 1594830, 4078465, 287036, 1643156,STACK, 2, 1357147, 1115, 241630, 1357147, 1065, 120815, 0
1,46,27, 201,[STACK | stackoverflow_userother], 1, 4078465, 286991, 1594830, 4078465, 287036, 1643156,STACK, 2, 1357147, 1115, 241630, 1357147, 1065, 120815, 0
All lines above should be ignored because STACK
is either in the 5th column, or 13th or in both.
This below is my actual function:
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));
}
Please help me out how I can do this on my current function. Thanks!
The following stack question will provide you with a rundown about the best way to read the last few lines of your file, I saw your comments.
What is the best way to read lines from end of file
With regard to the actual processing, it's CSV so check out the following inbuilt PHP function to create your array:
once you have that sorted:
/* This is just a one-liner to parse the file */
$aryCSV = array_map('str_getcsv', file('data.csv'));
foreach($aryCSV as $aryRow) {
/* This is your "ignore check" */
if(
$aryRow[5] == 'STACK' &&
$aryRow[13] == 'STACK'
) {
/* Go to next iteration */
continue;
}
/* Do your template loading and data saving */
}