Tuesday, 31 July 2012

Creating files based on the line number for printing


  1. // the lines from which will be read
  2. $lines = "1
  3. 5
  4. 16
  5. 26";
  6. // split these numbers above into an array
  7. $lines = explode(PHP_EOL, $lines);
  8.  
  9. // the source file where the ranges will be taken off
  10. $file = file('file.txt');
  11.  
  12.  
  13. for($i = 0; $i < count($lines); $i+=2) {
  14.   $rangeStart  = $lines[$i];
  15.   $rangeLength = $lines[$i+1] - $lines[$i];
  16.  
  17.   // slice out the ranges, beware the second parameter is the length not the offset!
  18.   $ranges[] = array_slice($file, $rangeStart, $rangeLength);
  19.  
  20. }
  21.  
  22. print_r($ranges);
  23.        
  24. // the lines from which will be read
  25. $lines = "1
  26. 5
  27. 16
  28. 26";
  29. // split these numbers above into an array
  30. $lines = explode(PHP_EOL, $lines);
  31.  
  32. // the source file where the ranges will be taken off
  33. $file = file('file.txt');
  34.  
  35.  
  36. for($i = 0; $i < count($lines); $i+=2) {
  37.   $rangeStart  = $lines[$i];
  38.   $rangeLength = $lines[$i+1] - $lines[$i];
  39.  
  40.   // slice out the ranges, beware the second parameter is the length not the offset!
  41.   $ranges[] = array_slice($file, $rangeStart, $rangeLength);
  42.  
  43. }
  44.  
  45. print_r($ranges);
  46.        
  47. // the lines from which will be read
  48. $lines = "1
  49. 5
  50. 16
  51. 26";
  52. // split these numbers above into an array
  53. $lines = explode(PHP_EOL, $lines);
  54.  
  55. // the source file where the ranges will be taken off
  56. $file = file('file.txt');
  57.  
  58.  
  59. for($i = 0; $i < count($lines); $i+=2) {
  60.   $rangeStart  = $lines[$i];
  61.   $rangeLength = $lines[$i+1] - $lines[$i];
  62.  
  63.   // slice out the ranges, beware the second parameter is the length not the offset!
  64.   $ranges[] = array_slice($file, $rangeStart, $rangeLength);
  65.  
  66. }
  67.  
  68. print_r($ranges);
  69.        
  70. // the lines from which will be read
  71. $lines = "1
  72. 5
  73. 16
  74. 26";
  75. // split these numbers above into an array
  76. $lines = explode(PHP_EOL, $lines);
  77.  
  78. // the source file where the ranges will be taken off
  79. $file = file('file.txt');
  80.  
  81.  
  82. for($i = 0; $i < count($lines); $i+=2) {
  83.   $rangeStart  = $lines[$i];
  84.   $rangeLength = $lines[$i+1] - $lines[$i];
  85.  
  86.   // slice out the ranges, beware the second parameter is the length not the offset!
  87.   $ranges[] = array_slice($file, $rangeStart, $rangeLength);
  88.  
  89. }
  90.  
  91. print_r($ranges);
  92.        
  93. $file = file('file.txt');
  94. $len  = count($file);
  95.  
  96. $output  = array();
  97. $current = array();
  98. for($i = 0; $i < $len; $i++) {
  99.   $data = trim($file[$i]);
  100.  
  101.   if ($data != 'BREAK') {
  102.     $current[] = $data;
  103.   } else {
  104.     $output[] = implode(PHP_EOL, $current);
  105.     $current  = array();
  106.   }
  107. }
  108.  
  109. print_r($output);

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...

Popular Posts