title: Removing Lines From CSV File Using PHP
body: <p>I am using the following code to pull a CSV file from a website that i do not have control over. and many of times i get the undefined index or headers already sent but all the data is there at the bottom. i want to write a script to open the file and remove all lines until it gets to the actual header line that should be in a csv.</p>
  
  <p>the # of lines changes every time i pull it...</p>
  
  <p>the current example has 49107 lines that i don't need before the part i want to parse.. This is a small part of the first 15 lines of code and about 20 lines of code before what i REALLY WANT from the file.</p>
  
  <pre><code>&lt;pre class="cake-debug"&gt;&lt;a href="javascript:void(0);" onclick="document.getElementById('cakeErr1-trace').style.display = (document.getElementById('cakeErr1-trace').style.display == 'none' ? '' : 'none');"&gt;&lt;b&gt;Notice&lt;/b&gt; (8)&lt;/a&gt;: Undefined index: name [&lt;b&gt;APP/controllers/loads_controller.php&lt;/b&gt;   line &lt;b&gt;327&lt;/b&gt;]&lt;div id="cakeErr1-trace" class="cake-stack-trace" style="display: none;"&gt;&lt;a href="javascript:void(0);" onclick="document.getElementById('cakeErr1-code').style.display = (document.getElementById('cakeErr1-code').style.display == 'none' ? '' : 'none')"&gt;Code&lt;/a&gt; | &lt;a href="javascript:void(0);" onclick="document.getElementById('cakeErr1-context').style.display = (document.getElementById('cakeErr1-context').style.display == 'none' ? '' : 'none')"&gt;Context&lt;/a&gt;&lt;div id="cakeErr1-code" class="cake-code-dump" style="display: none;"&gt;&lt;pre&gt;&lt;code&gt;&lt;span style="color: #000000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;$data[$i]['Load']['drop_date']&amp;nbsp;=&amp;nbsp;date('m/d/Y' &amp;nbsp;strtotime($value['Load']['drop']));&lt;/span&gt;&lt;/code&gt;                                         
  &lt;code&gt;&lt;span style="color: #000000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;$data[$i]['Load']['pickup_city']&amp;nbsp;=&amp;nbsp;$value['Pickup']['city'];&lt;/span&gt;&lt;/code&gt;                                                 
  "&lt;span class=""code-highlight""&gt;&lt;code&gt;&lt;span style=""color: #000000""&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;$data[$i]['Load']['pickup_state']&amp;nbsp;=&amp;nbsp;$value['Pickup']['State']['name'];&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;pre id=""cakeErr1-context"" class=""cake-context"" style=""display: none;""&gt;$order   =   ""Load.load_number ASC"""                                                   
  "$fields    =   array("                                                 
  "   ""*"""                                                  
  )                                                   
  "$conditions    =   array("                                                 
  "   ""Load.active"" =&gt; true"                                                    
  )                                                   
  "$results   =   array("                                                 
  "   array("                                                 
  "   ""Load"" =&gt; array()"                                                    
  "   ""Pickup"" =&gt; array()"                                                  
  "   ""Destination"" =&gt; array()"                                                 
  )                   
  $result =   array(
      "Load" =&gt; array(
      "name" =&gt; "ICE CREAM OR RELATED",
      "load_number" =&gt; "8891517",
      "trailer_type" =&gt; "R",
      "phone_number1" =&gt; "800-555-8287",
      "phone_number2" =&gt; "800-555-8287",
      "pickup_date" =&gt; "03/09/2014",
      "drop_date" =&gt; "03/09/2014",
      "pickup_city" =&gt; "Indianapolis",
      "pickup_state" =&gt; "Indiana",
      "pickup_zipcode" =&gt; "46201",
      "destination_city" =&gt; "London",
      "destination_state" =&gt; "Kentucky",
      "destination_zipcode" =&gt; "40741"
  )
  )
  
  
  $fp=&lt;/pre&gt;&lt;pre class="stack-trace"&gt;header - [internal], line ??
  LoadsController::csv() - APP/controllers/loads_controller.php, line 360
  Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 204
  Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 170
  [main] - APP/webroot/index.php, line 83&lt;/pre&gt;&lt;/div&gt;
  &lt;/pre&gt;name,load_number,trailer_type,phone_number1,phone_number2,pickup_date,drop_date,pickup_city,pickup_state,pickup_zipcode,destination_city,destination_state,destination_zipcode
      "FOOD OR KINDRED PROD",8831029,R,800-555-8287,800-555-8287,03/09/2014,03/10/2014,Aurora,Illinois,60504,"West Memphis",Arkansas,72301
      "FOOD OR KINDRED PROD",8831031,R,800-555-8287,800-555-8287,03/12/2014,03/13/2014,Aurora,Illinois,60504,Ashley,Indiana,46705 
  </code></pre>
  
  <p>This is how I would like the file to look after removing the lines that should not be there...</p>
  
  <pre><code>name,load_number,trailer_type,phone_number1,phone_number2,pickup_date,drop_date,pickup_city,pickup_state,pickup_zipcode,destination_city,destination_state,destination_zipcode
  FOOD OR KINDRED PROD,8831029,R,800-555-8287,800-555-8287,3/9/2014,3/10/2014,Aurora,Illinois,60504,West Memphis,Arkansas,72301
  FOOD OR KINDRED PROD,8831031,R,800-555-5555,800-555-5555,3/12/2014,3/13/2014,Aurora,Illinois,60504,Ashley,Indiana,46705
  </code></pre>
  
  <p>Currently i am using this code to get my CSV</p>
  
  <pre><code>set_time_limit (24 * 60 * 60);
  
  // folder to save downloaded files to. must end with slash
  $destination_folder = 'downloads/';
  
  $url = 'http://www.somesite.com/loads/csv';
  $newfname = $destination_folder . 'loads1.csv';
  
  $file = fopen ($url, "rb");
  if ($file) {
    $newf = fopen ($newfname, "wb");
  
    if ($newf)
    while(!feof($file)) {
      fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
    }
  }
  
  if ($file) {
    fclose($file);
  }
  
  if ($newf) {
    fclose($newf);
  }
  </code></pre>
  
  <p>and this Code to parse it</p>
  
  <pre><code>$selectfile1 = "https://www.somesite.com/downloads/loads1.csv";
      // check mime type - application/octet-stream
  
      $content = file($selectfile1);
  
      $posted_content = array();
      list($rownum, $row) = each($content);
      $posted_content[0] = explode(",", $row);
      array_push($posted_content[0], "ID");
      $count = 0;
      // iterate each row (1 post)
      while (list($rownum, $row) = each($content))
      {
          $count++;
  
          $cols = "ShipAfterDate, ShipBeforeDate, EquipmentID, LengthID, VendorCode, LoadCount, Rate, CargoDescription, Notes,Phone1, Phone2, PostDate,";
          $vals = "";
          // extract fields from row columns
          $items = explode(",", $row);
          list( $Description, $OrderNumber, $EquipmentCode, $Phone1, $Phone2, $ShipDate, $DeliveryDate, $OriginCity, $OriginState, $OriginZip, $DestinationCity, $DestinationState, $DestinationZip
              ) = $items;
  
  
          array_push($posted_content, $items);
  </code></pre>