Jump to content

Removing lines from text file read


ProgrammerJen

Recommended Posts

I have the following text file:

09/16/2011 - 09/16/2011      323780048      FedEx Ground Shipment Detail Report      09/22/2011      CAFE2472     Page: 1
TRACKING #       RECIPIENT CD    ZIP    ZN  CARRIER   SVC BILL WGT COD        DECL VALUE C GR CHG   C SRCHG    C NET      
---------------- --------------- ------ --- ------- - --- -------- ---------- ---------- ---------- ---------- ---------- 
561895962528473  97913           440607 4   Ground  , RH       2.0       0.00      99.00       6.22       2.45       8.27 
561895962528480  97928           352424 5   Ground  , RH      17.0       0.00      99.00       9.89       2.45      10.60 
561895962528497  97943           857164 6   Ground  , RH      17.0       0.00      99.00      12.38       2.45      14.75 
561895962528503  97955           193825 5   Ground  , RH       4.0       0.00      99.00       7.17       2.45       8.42 
561895962528510  6792            491069 3   Ground  , RH       3.0       0.00      99.00       5.97       2.45       8.27 
561895962528527  97967           030703 5   Ground  , RH       1.0       0.00      99.00       5.75       4.10      10.06 
561895962528534  97978           665429 4   Ground  , RH       4.0       0.00      99.00       6.86       4.10      10.06 
561895962528541  97992           371844 4   Ground  , RH       1.0       0.00      99.00       5.51       4.25      10.22 
561895962528558  98005           386108 4   Ground  , RH      18.0       0.00      99.00       9.21       4.25      12.01 

GRAND TOTALS
COURTESY NET CHARGE                      92.66
TOTAL WEIGHT(OF LBS)                      63.7
TOTAL WEIGHT(OF KGS)                       0.0
CSY  DECL VAL SCHG                        0.00
COURTESY DISCOUNT                        12.52
COURTESY SPECIAL FEES                    28.95
CSY FUEL SURCHARGE                        7.27
PACKAGE COUNT                                9

 

And I'm attempting to pull ONLY the tracking number and reference number (or order #) from this file. I've been able to get rid of the empty lines, get rid of the first few lines I don't need and grab just tracking and reference numbers , but I can' t figure out how to get rid of of the last few lines....here's my code:

 

$arr=file("/home/onest4/public_html/beta/fedex.txt");
$x=1;
    foreach($arr as $str)
    {
      if (trim($str) != '')
      if ($x > 3)

      
      {
        list($tracking,$ordernumber)=explode("  ",$str);

        echo $tracking;
        echo "<br />";
        echo $ordernumber;

        echo "<br /><br />";
      }
      else
      {
      $x++;
      continue;
      }
    }

 

Here's the result:

561895962528473
97913

561895962528480
97928

561895962528497
97943

561895962528503
97955

561895962528510
6792

561895962528527
97967

561895962528534
97978

561895962528541
97992

561895962528558
98005

GRAND TOTALS


COURTESY NET CHARGE


TOTAL WEIGHT(OF LBS)


TOTAL WEIGHT(OF KGS)


CSY
DECL VAL SCHG

COURTESY DISCOUNT


COURTESY SPECIAL FEES


CSY FUEL SURCHARGE


PACKAGE COUNT


 

Anyone help with this?

 

Thanks!

Link to comment
Share on other sites

try this: (untested)

<?php
$arr=file("/home/onest4/public_html/beta/fedex.txt");
$x=1;
foreach($arr as $str){
if (trim($str) != ''){
	if ($x > 3){
		list($tracking,$ordernumber)=explode("  ",$str);
			if(ctype_digit($tracking)){
				echo $tracking."<br />".$ordernumber."<br /><br />";
			}
	}else{
		$x++;
		continue;
	}
}
?>

 

I basically added if(ctype_digit($tracking)){ to check if the variable contains only digits, otherwise it's ignored.

Link to comment
Share on other sites

I would think FedEx would have an existing API to allow you to get the data you need without trawling through a text file, but this should work too, if the file is in that format every time (otherwise YMMV).

<?php
$arr=file('/home/onest4/public_html/beta/fedex.txt', FILE_SKIP_EMPTY_LINES);
unset( $arr[0] );
foreach($arr as $k => $v) {
$v = trim($v);
if( !empty($v) ) {
	if( ctype_digit($v[0]) ) {
		list($tr, $or) = explode( ' ', preg_replace('~\s{2,}~', ' ', $v) );
		echo "Tracking#: $tr<br>Order#: $or<br><br>";
	}
}
}

Link to comment
Share on other sites

Ok folks, you were alot of help with reading the text file...I'm creating the rest of my file now, and need some help...

I've created a function to update the database with the information from the text file...I've already got additional code (not posted here) that displays the orders that have NOT been shipped (and works beautifully). Then when they click the button it should process the text file and update the database (input the tracking number into the database) and change the order status and send an email to the customer, I know I've got something mixed up because it's not working...

 

 

function process_shipping($ordernumber, $status=''){

      $order_updated = false;

 

    $check_status_query = tep_db_query("select customers_name, customers_email_address, orders_status, date_purchased from " . TABLE_ORDERS . " where orders_id = '" . $ordernumber . "'");

  $check_status = tep_db_fetch_array($check_status_query);

 

  $orders_statuses = array();

  $orders_status_array = array();

  $orders_status_query = tep_db_query("select orders_status_id, orders_status_name from " . TABLE_ORDERS_STATUS . " where language_id = 1");

  while ($orders_status = tep_db_fetch_array($orders_status_query)) {

    $orders_statuses[] = array('id' => $orders_status['orders_status_id'],

                              'text' => $orders_status['orders_status_name']);

    $orders_status_array[$orders_status['orders_status_id']] = $orders_status['orders_status_name'];

  }

 

  if ($status!==''){  

    if ($check_status['orders_status'] !== $status) {

        tep_db_query("update " . TABLE_ORDERS . " set fedex_track_num = '" . $tracking . "'orders_status = '". $status . "', last_modified = now() where orders_id = '" . $oID . "'");

        $customer_notified = '0';

 

        $notify_comments = '';

          $email = STORE_NAME . "\n" . EMAIL_SEPARATOR . "\n" . EMAIL_TEXT_ORDER_NUMBER . ' ' . $oID . "\n" . EMAIL_TEXT_INVOICE_URL . ' ' . tep_catalog_href_link(FILENAME_CATALOG_ACCOUNT_HISTORY_INFO, 'order_id=' . $oID, 'SSL') . "\n" . EMAIL_TEXT_DATE_ORDERED . ' ' . tep_date_long($check_status['date_purchased']) . "\n\n" . $notify_comments . sprintf(EMAIL_TEXT_STATUS_UPDATE, $orders_status_array[$status]);

          tep_mail($check_status['customers_name'], $check_status['customers_email_address'], EMAIL_TEXT_SUBJECT, nl2br($email), STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);

        $customer_notified = '1';

       

tep_db_query("insert into " . TABLE_ORDERS_STATUS_HISTORY . " (orders_id, orders_status_id, date_added, customer_notified, comments) values ('" . (int)$oID . "', '" . tep_db_input($status) . "', now(), '" . tep_db_input($customer_notified) . "', '" . tep_db_input($comments)  . "')");

 

        $order_updated = true;

      }

  }

 

  }

 

 

 

if ($_POST['update']=='1') {

echo '<div class="dataTableContent" align="left">';

$arr=file("/home/onest4/public_html/beta/fedex.txt");

$x=1;

 

foreach($arr as $str){

if (trim($str) != ''){

if ($x > 3){

list($tracking,$oID)=explode("  ",$str);

if(ctype_digit($tracking)){

 

echo "Order# $oID, <b>$tracking</b> (DATABASE UPDATED)<br>";

}

 

}else{

$x++;

continue;

}

}

}

}

 

 

Probably just been staring at this too long like the other night with the file read...but any help would be much appreciated!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.