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.
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.
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.
Like I said, use fread() and parse it yourself. preg_match() would work real good for that.
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
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.
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.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)