/    Sign up×
Community /Pin to ProfileBookmark

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.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@temp_user123Jul 03.2007 — Depends. Is the file considered a [B]CSV [/B]file? ...i.e., can it be opened correctly in [B]Excel[/B]? If so, then [B]fgetcsv()[/B] may work just fine for you. If not, you may have to use [B]fread()[/B] and parse it yourself.
Copy linkTweet thisAlerts:
@kl1980authorJul 03.2007 — 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

[upl-file uuid=2446e12c-6166-45f7-8718-2a4e5d4f0f12 size=719B]example.txt[/upl-file]
Copy linkTweet thisAlerts:
@temp_user123Jul 03.2007 — Like I said, use [B]fread()[/B] and parse it yourself. [B]preg_match()[/B] would work real good for that.
Copy linkTweet thisAlerts:
@hastxJul 04.2007 — 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?
Copy linkTweet thisAlerts:
@bluestarsJul 04.2007 — 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
Copy linkTweet thisAlerts:
@temp_user123Jul 04.2007 — I downloaded the file. There are both multiple spaces and multiple tabs between some data elements. Like I said, [B]preg_match()[/B] 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 [B]preg_match_all()[/B] instead. ?
×

Success!

Help @kl1980 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...