문제

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.

the # of lines changes every time i pull it...

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.

<pre class="cake-debug"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr1-trace').style.display = (document.getElementById('cakeErr1-trace').style.display == 'none' ? '' : 'none');"><b>Notice</b> (8)</a>: Undefined index: name [<b>APP/controllers/loads_controller.php</b>   line <b>327</b>]<div id="cakeErr1-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr1-code').style.display = (document.getElementById('cakeErr1-code').style.display == 'none' ? '' : 'none')">Code</a> | <a href="javascript:void(0);" onclick="document.getElementById('cakeErr1-context').style.display = (document.getElementById('cakeErr1-context').style.display == 'none' ? '' : 'none')">Context</a><div id="cakeErr1-code" class="cake-code-dump" style="display: none;"><pre><code><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data[$i]['Load']['drop_date']&nbsp;=&nbsp;date('m/d/Y' &nbsp;strtotime($value['Load']['drop']));</span></code>                                         
<code><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data[$i]['Load']['pickup_city']&nbsp;=&nbsp;$value['Pickup']['city'];</span></code>                                                 
"<span class=""code-highlight""><code><span style=""color: #000000"">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data[$i]['Load']['pickup_state']&nbsp;=&nbsp;$value['Pickup']['State']['name'];</span></code></span></pre></div><pre id=""cakeErr1-context"" class=""cake-context"" style=""display: none;"">$order   =   ""Load.load_number ASC"""                                                   
"$fields    =   array("                                                 
"   ""*"""                                                  
)                                                   
"$conditions    =   array("                                                 
"   ""Load.active"" => true"                                                    
)                                                   
"$results   =   array("                                                 
"   array("                                                 
"   ""Load"" => array()"                                                    
"   ""Pickup"" => array()"                                                  
"   ""Destination"" => array()"                                                 
)                   
$result =   array(
    "Load" => array(
    "name" => "ICE CREAM OR RELATED",
    "load_number" => "8891517",
    "trailer_type" => "R",
    "phone_number1" => "800-555-8287",
    "phone_number2" => "800-555-8287",
    "pickup_date" => "03/09/2014",
    "drop_date" => "03/09/2014",
    "pickup_city" => "Indianapolis",
    "pickup_state" => "Indiana",
    "pickup_zipcode" => "46201",
    "destination_city" => "London",
    "destination_state" => "Kentucky",
    "destination_zipcode" => "40741"
)
)


$fp=</pre><pre class="stack-trace">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</pre></div>
</pre>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 

This is how I would like the file to look after removing the lines that should not be there...

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

Currently i am using this code to get my CSV

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);
}

and this Code to parse it

$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);
도움이 되었습니까?

해결책

Check out 'fgetcsv' (PHP manual) which just returns false if there's a parse error or the actual CSV values if not. It might be not the fastest solution to unsuccessfully parse 50k lines, but I think it should work nevertheless

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top