Guest User

Untitled

a guest
Jul 19th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. <?php
  2. $row = 1;
  3. $handle = fopen("test.csv", "r");
  4. while ($data = fgetcsv($handle, 1000, ","))
  5. {
  6. if (preg_match('/[Morning]/', $data[0]) === 1 // start at this rwo plus two lines down )
  7. {
  8. $num = count($data);
  9. $row++;
  10. for ($c=0; $c < $num; $c++)
  11. {
  12. for ($c=0; $c < $num; $c++)
  13. {
  14. echo $data[$c] . " ";
  15. }
  16. if (preg_match('/[Total Cash:]/', $data[0]) === 1)
  17. { break; row -1 }
  18. }
  19. echo "<br>";
  20. }
  21. }
  22. fclose($handle); ?>
  23.  
  24. &#47;&#91;Morning&#93;&#47;
  25.  
  26. $lines = file('test.csv'); //read file into an array, one entry per line
  27.  
  28. $active = false; //keep track of what rows to parse
  29.  
  30. //loop one line at a time
  31. for ($i = 0; $i < count($lines); $i++) {
  32. $line = $lines[$i];
  33.  
  34. if (strpos($line, 'Morning') !== false) { //start parsing on the next row
  35. $active = true;
  36. $i += 2; //skip the blank line and header
  37. continue;
  38. }
  39. if (strpos($line, '----,') !== false) { //stop parsing rows
  40. $active = false;
  41. }
  42.  
  43. if ($active) { //if parsing enabled, split the line on commas and do something with the values
  44. $values = str_getcsv(trim($line));
  45. foreach ($values as $value) {
  46. echo $value . " "; //these are the numbers
  47. }
  48. }
  49.  
  50. }
  51.  
  52. $lines = file('test.csv');
  53. $parsing = false;
  54. foreach ($lines as $line)
  55. {
  56. $parsing = ((strpos($line, 'Morning') !== false) || $parsing)
  57. && ((strpos($line, 'Total Cash') === false);
  58.  
  59. if (!$parsing)
  60. continue;
  61.  
  62. $values = strgetcsv($line);
  63. echo implode(' ', $values);
  64. }
  65.  
  66. $lines = file('test.csv');
  67.  
  68. // Skip the unwanted lines
  69. // Means: Every line until the line containing "Morning,"
  70. do {
  71. $line = array_shift($lines);
  72. } while(trim($line) !== 'Morning,');
  73. $lines = array_slice($lines, 2); // Mentioned something about "2 lines below" or such" ^^
  74.  
  75. // Do something with the remaining lines, until
  76. // Line _begins_ with "Total Cash"
  77. while(strncmp('Total Cash', trim($line = array_shift($lines)), 10) !== 0) {
  78. echo implode(' ', str_getcsv($line)) . PHP_EOL;
  79. }
Add Comment
Please, Sign In to add comment