Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $row = 1;
- $handle = fopen("test.csv", "r");
- while ($data = fgetcsv($handle, 1000, ","))
- {
- if (preg_match('/[Morning]/', $data[0]) === 1 // start at this rwo plus two lines down )
- {
- $num = count($data);
- $row++;
- for ($c=0; $c < $num; $c++)
- {
- for ($c=0; $c < $num; $c++)
- {
- echo $data[$c] . " ";
- }
- if (preg_match('/[Total Cash:]/', $data[0]) === 1)
- { break; row -1 }
- }
- echo "<br>";
- }
- }
- fclose($handle); ?>
- /[Morning]/
- $lines = file('test.csv'); //read file into an array, one entry per line
- $active = false; //keep track of what rows to parse
- //loop one line at a time
- for ($i = 0; $i < count($lines); $i++) {
- $line = $lines[$i];
- if (strpos($line, 'Morning') !== false) { //start parsing on the next row
- $active = true;
- $i += 2; //skip the blank line and header
- continue;
- }
- if (strpos($line, '----,') !== false) { //stop parsing rows
- $active = false;
- }
- if ($active) { //if parsing enabled, split the line on commas and do something with the values
- $values = str_getcsv(trim($line));
- foreach ($values as $value) {
- echo $value . " "; //these are the numbers
- }
- }
- }
- $lines = file('test.csv');
- $parsing = false;
- foreach ($lines as $line)
- {
- $parsing = ((strpos($line, 'Morning') !== false) || $parsing)
- && ((strpos($line, 'Total Cash') === false);
- if (!$parsing)
- continue;
- $values = strgetcsv($line);
- echo implode(' ', $values);
- }
- $lines = file('test.csv');
- // Skip the unwanted lines
- // Means: Every line until the line containing "Morning,"
- do {
- $line = array_shift($lines);
- } while(trim($line) !== 'Morning,');
- $lines = array_slice($lines, 2); // Mentioned something about "2 lines below" or such" ^^
- // Do something with the remaining lines, until
- // Line _begins_ with "Total Cash"
- while(strncmp('Total Cash', trim($line = array_shift($lines)), 10) !== 0) {
- echo implode(' ', str_getcsv($line)) . PHP_EOL;
- }
Add Comment
Please, Sign In to add comment