#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    19
    Rep Power
    0

    Parse EDI with PHP/MySql


    Hi! I have a 40K line EDI (fixed length) file that I must parse into a csv. If you know edi you know that each element has its own fixed length.

    Im new to parsing EDI and just need a bit of help. My first thought is to set up a table that will hold the element lengths either as an array or as such

    Code:
    Table EDIInfo
    EDI_ID           |  EDI_ElemLengths
    
    1                |  3,22,7s2,30,30,22
    2                |  30,5s2,9s2,3,1,23
    ** the s in the array above denotes a decimal ofter the second place from right.

    So once I get this data into the db, Im not sure how to pull it out to apply it to the file that I have located on my server. The file is extensionless at this point, its a simple text file. Id like to parse it into a new file XXXX.csv in the same directly.

    Any links to tuts or any help/direction would be greatly appreciated.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,332
    Rep Power
    594
    If you could clarify a bit more, why do you want to put EDI formatted data into a database? It seems to me that you would parse the EDI data then put the normalized result into the database and/or your csv file.

    As for parsing the data, you are correct. You would read each line into an array based on the delimiters and then normalize the data. It is my understanding that there have been numerous attempts to create PHP parsers for EDI but they tend to be abandoned since although the structure of EDI is relatively simple conversion of the data itself is unique to each data file.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    19
    Rep Power
    0
    The EDI data itself will not be in the DB. The Element lengths will.
    My thought (thought about it in my sleep and going to try it tonite) is this.
    1) read each segment as a string
    2) explode the segment based on the value of each element length that I have stored in a mysql table
    3) strip blank spaces from the exploded results (element data does not always take up the entire element space) and replace with a delimiter like '^'
    4) save as a delimited file.
    Does that seem logically sound?

    Thank you for your input.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,332
    Rep Power
    594
    Sounds fine to me.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo