kl1980
07-03-2007, 03:01 PM
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.
Thanks in advance.
|
Click to See Complete Forum and Search --> : how to read multiple delimited files kl1980 07-03-2007, 03:01 PM 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. temp.user123 07-03-2007, 03:07 PM 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. kl1980 07-03-2007, 03:19 PM 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 temp.user123 07-03-2007, 03:25 PM Like I said, use fread() and parse it yourself. preg_match() would work real good for that. hastx 07-03-2007, 11:20 PM 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? bluestars 07-04-2007, 11:28 AM 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 temp.user123 07-04-2007, 01:56 PM 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. ;) webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |