Jump to content

PHP: EDI Thinner

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Guest_Jordan_*

Guest_Jordan_*
  • Guests
The code below will take an EDI document and remove all lines where the segment is not in the allowedSegments Array. It was made for and runs in PHP Version 4.

#!/usr/bin/php -Cq
<?php
/**
 * Author: Jordan DeLozier
 * Date: 9-16-2008
 * Version: 1.0
 * 
 * Description: Remove all line items from an
 * EDI document except those allowed.
 * 
 */

// Require our functions script
require("edi_functions.php");

// Define our arguments
$input      = $argv[1];
$output     = $argv[2];

// Allowed segments
$allowedSegments = ARRAY('ISA',
                         'GS',
                         'ST',
                         'SE',
                         'GE',
                         'IEA');


// Read our file
$edi = readInput($input);

// Find the terminator
$segTerm = substr($edi,105,1);
$segSep  = "\\" . substr($edi,3, 1);

// Parse the EDI into an array
// Use default delimiter
$ediArray = parseEDI($edi, $segTerm);
$newEdiArray = "";

// Loop through the results and build
// the output file based on the ISA/GS/ST
// records
for ($i=0; $i<count($ediArray); $i++) {
        // Declare variables
        $line      = $ediArray[$i];
        $ediLine = parseEDI($line, $segSep);
        $segment = trim($ediLine[0]);
    
        // If the values are not in the segment
        // array then we do not want to print them.
        if (in_array($segment, $allowedSegments)) {
            // Value is allowed, write it to output
            $newEdiArray[] = $line;
        }
}

// Write the final EDI output
writeOutput($output, implode($segTerm, $newEdiArray));


?>
and the ediFunctions file:
<?php

/**
 * Author: Jordan DeLozier
 * Date: July 2nd, 2008
 * Version: 1.0
 * 
 * Description: Functions for EDI Pre
 * and post processors..
 * 
 */

/**
 * Read contents from the EDI document
 *   text file.
 *
 * @param string $input
 */
function readInput($input) {
    $fh = fopen($input, 'r') or die("Can't open input file!");
    $contents = fread($fh, filesize($input));
    fclose($fh);

    // Return the string
    return $contents;
}

/**
 * Splits a string based on a delimiter
 * and returns the array.
 *
 * @param String $edi
 * @param char $delim
 * @return Array
 */
function parseEDI($edi, $delim="'") {
    return split($delim, $edi);
}

/**
 * Insert an array element
 *
 * @param array  $array
 * @param string $insert
 * @param int    $position
 */
function array_insert(&$array, $insert, $position) {
    return array_splice($array,$position,0,$insert);
}

/**
 * Write our finale data to the output file
 * select by the user
 *
 * @param string $output - File Path
 * @param string $text - Content to write
 */
function writeOutput($output, $text) {
    $fh = fopen($output, 'w') or die("can't open file");
    fwrite($fh, $text);
    fclose($fh);
}
?>

Example Usage:
# ./ediThinner.php <input> <output>

Example Input:
ISA*00*          *00*          *01*SOMEDUNS      *01*ANOTHERDUNS      *080916*0431*U*00401*000000691*0*P*>
GS*PS*GSLOC*GSLOC2*20080916*0431*691*X*004010
ST*830*0001
BFR*05*20080916043138*20080916043138*DL*A*20080718*20091124*20080915
N1*ST**1*A33
LIN*00010*BP*1234343*CR*PO1009
UIT*PC*10751*TC
CUR*BY*USD
REF*DP*SVC1
FST*5*D*D*20090313
FST*5*D*D*20090612
FST*2*D*D*20090911
SHP*01*10*050*20080423
REF*SI*682643
SHP*02*85*051*20030909
CTT*1*12
SE*18*0001
ST*830*0002
BFR*05*20080916043138*20080916043138*DL*A*20080718*20091124*20080915
N1*ST**1*A32
LIN*00010*BP*373333*VP*11109*EC*A*CR*XK09123
UIT*PC*5556*TC
CUR*BY*USD
REF*DP*SVC1
FST*5*D*D*20081103
FST*5*D*D*20090202
FST*5*D*D*20090401
FST*5*D*D*20090601
FST*5*D*D*20090701
SHP*01*20*050*20080219
REF*SI*678651
SHP*02*155*051*20031030
CTT*1*25
SE*20*0002
GE*2*691
IEA*1*000000691

Output:
ISA*00*          *00*          *01*SOMEDUNS      *01*ANOTHERDUNS      *080916*0431*U*00401*000000691*0*P*>
GS*PS*GSLOC*GSLOC2*20080916*0431*691*X*004010
ST*830*0001
SE*18*0001
ST*830*0002
SE*20*0002
GE*2*691
IEA*1*000000691


#2
morefood2001

morefood2001

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,720 posts
So does the output do exactly what the input does with fewer lines?

And for example, what does:
BFR*05*20080916043138*20080916043138*DL*A*20080718*20091124*20080915
exactly do?

#3
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I made this for a special purpose. Basically it removes all lines that you do not want.

BFR*05*20080916043138*20080916043138*DL*A*20080718*20091124*20080915
BFR doesn't actually "do" anything. It is all informational. For example, the "05" is the purpose code and 05 means replacement; 20080916043138 is a date/time stamp.

#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Nice code, but I don't see any error checking. You might want to add something like:

if($argc != 3) {
    die("Missing arguments");
}


#5
Guest_Jordan_*

Guest_Jordan_*
  • Guests
For what I use this for it is critical that nothing half the next workflow step, such as a die function. Not a bad suggestion for others though. Thanks.