I need a little help figurig out the best method to do something...

I have written a PHP program that allows the user of a site I am building to upload a tab delimited file (CSV) to his site, PHP parses the file using fgetcsv() and then puts the data into his database. It works great.

HOWEVER...
I guess, some of this guy's files that he wants to process are given to him in SPACE DELIMITED (as in the columns all line up in monospaced type) files rather than tab or comma delimited format. They are also too big to load into excel and convert to CSV format.

SO...
I need to make a way for him to upload and process them in the original format of space delimited.

QUESTION...
is fgetcsv() still the best function to use for this, and if so, what "delimiter" charactor do I put in the function? (what would be the equivalent for multiple spaces of various lengths?)... or is there a different function or set of functions I should use? I see that there is other "get" functions like fgetc, fgets, fgetss... and I htink that in conjection with an explode oe implode function I could parse out the spaces and get the data into order... but I have searced php sites and Google for examples of how to do this and have found nothing good to go off of. Does anyone have an example or a concept of how to do this best?

Thanks!

GF

    I think a new script is in order here.

    Something using substr() and trim() .

    Halfabee

      Hi,
      look to explode() function.

      $mydata=explode(" ",file("myfile.txt"));

      I hope this helps

        Thanks!

        I got it to work by reading in the lines of data from the file using fgets()... and then getting the two data elements (part number and quantity) out by doing this...

        // clip first 27 char off and remove spaces at ends to make itemnumber
        $itemnumber=trim(substr($data, 0, 27));

        // clip off substring last 8 char and clean off spaces to get inv qty
        $quantity=trim(substr($data, -8, 8));

        that did the trick... I guess I don't know why I could not figure that out on my own!

        Thanks again!!

          $quantity=trim(substr($data, -8, 8));

          I expect this just goes to show that 2 heads are better tnan one.
          Or in this case halfahead is better than two. 😃

          Halfabee

            Write a Reply...