www.webdeveloper.com
Results 1 to 7 of 7

Thread: how to read multiple delimited files

  1. #1
    Join Date
    Jun 2007
    Posts
    31

    Question how to read multiple delimited files

    I have to write a script that will read a multiple delimited file (one or more tabs, one or more space or a mix of space and tab). How to do this in PHP.

    Thanks in advance.

  2. #2
    Join Date
    Jun 2007
    Posts
    348
    Depends. Is the file considered a CSV file? ...i.e., can it be opened correctly in Excel? If so, then fgetcsv() may work just fine for you. If not, you may have to use fread() and parse it yourself.

  3. #3
    Join Date
    Jun 2007
    Posts
    31

    Question

    NO it is just a text file. I have attached a sample file below. I have to read that file and store each field (ID, Text, RGB.....) in an array.

    How do I do that. I have read a file that is either tab delimited or space delimited, but not a multiple delimited file.

    Thanks,
    KL1980
    Attached Files Attached Files

  4. #4
    Join Date
    Jun 2007
    Posts
    348
    Like I said, use fread() and parse it yourself. preg_match() would work real good for that.

  5. #5
    Join Date
    May 2003
    Posts
    599
    Reading it is the easy part and there are a few ways to do it, what do you plan to do with the parsed data after you read it?
    We do precision guesswork

  6. #6
    Join Date
    Jun 2007
    Posts
    112
    Can there be spaces in that string?

    If not, you could just read to an array, then replace all TABs with spaces (or vice-versa), then explode on a space.

    If there are, you'd have to make up a regexp that matches all spaces NOT inside "", then replace those with tabs or semicolons or something, then explode it.

    http://us.php.net/explode

    http://us.php.net/manual/en/function.preg-replace.php

  7. #7
    Join Date
    Jun 2007
    Posts
    348
    I downloaded the file. There are both multiple spaces and multiple tabs between some data elements. Like I said, preg_match() will, in a single step, automatically give you an array of the data elements -- all stripped of all such white space characters. Actually, now that I think about it, you'd need to use preg_match_all() instead.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles