473,811 Members | 3,521 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

comma delineated files

Say I have an file whose contents are as follows:

"test","com,ma" ,"food"

How would I go about turning that file into an array, as follows?:

$file_contents = array('"test"', '"com,ma"','"fo od"');

Doing explode(',',$fi le_contents) wouldn't be sufficient because the
resultant array would have four entries - not three - and would instead
read like this:

$file_contents = array('"test"', '"com','ma"','" food"');

I could probably iterate through each character within the array, but
that, it seems to me, would be inefficient. Plus, it'd require more
code that'd be nice to avoid.

So, anyway, any ideas?

Nov 2 '05 #1
3 2292
try with preg_match_all( ) (http://php.net/preg_match_all)

Nov 2 '05 #2
I should, perhaps, also mention that strings aren't the only things
that can be between the commas. Another valid file is as follows:

"te,st",4,2

Given this, the regular expression is, at the very least, not going to
be particuraly easy. Before making the original post, I was unable to
come up with a sufficient one for preg_split.

As such, if regular expressions are the key, I could still use some
help...

Nov 2 '05 #3
yawnmoth said the following on 02/11/2005 01:52:
I should, perhaps, also mention that strings aren't the only things
that can be between the commas. Another valid file is as follows:

"te,st",4,2

Given this, the regular expression is, at the very least, not going to
be particuraly easy. Before making the original post, I was unable to
come up with a sufficient one for preg_split.

As such, if regular expressions are the key, I could still use some
help...


A shortcut might be http://php.net/fgetcsv.

--
Oli
Nov 2 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
5727
by: Deepa | last post by:
I am writing a console app in c# wherein am converting a dataset into a CSV file. It works fine. But I have some values in the dataset which have a comma within(eg. A,B,C). When I view the CSV file in Excel, the multiple values which are separated by the 'comma' in the dataset are being displayed in the next column , which I dont want that way. I want to retain any commas which are present in my dataset. How do I handle such a scenario? ...
4
1784
by: Ron McCafferty | last post by:
How do you read comma delimited data from programs such as Excel where the data itself contains dounble quotes and commas. It used to be you could just use input. How is it fone now? The data below contains output from Access saved as a CSV. line 1,"normal data","a double quote "" here" line 2,"a comma , here ","more normal data" Input's help documents the "" will not work.
3
5440
by: Elmo Watson | last post by:
I've been asked to develop a semi-automated type situation where we have a database table (sql server) and periodically, there will be a comma delimited file from which we need to import the data, replacing the old. I naurally know that we can use to kill the other data, but does anyone have any examples of importing a comma delimited file into SQL Server with ASP?
3
1881
by: SteelDetailer | last post by:
Thnaks in advance for considering this post. It's probably very simple, but..... I have an old VB6 application that allows me to create, save and edit a "project information file" that is a simple .txt file. I then use that file to keep the project information current for the multiple AutoCAD drawings that project requires. My problem.....several of the items I save are company names (customers). Many times company names have commas...
3
9722
by: Kim Bundgaard | last post by:
Hi Any suggestion would be fine. I have installed DB2 Connect EE V8.1 (fixpak 4) at two different Windows 2000 Servers, both have the same regional settings. I am connecting to the same DB2 z/OS subsystem running V7 from a DB2 Command Window, and I do a "select decimal-field from table" - but at machine-a I get the field with comma as decimal delimiter, and at machine-b I get period as decimal delimiter.
1
6699
by: John B. Lorenz | last post by:
I'm attempting to write an input routine that reads from a comma delimited file. I need to read in one record at a time, assign each field to a field array and then continue with my normal processing. I am having no luck at all finding different routines written in C to read delimited files of any kind. I have a few ideas of how I might go about this but I bet I'm re-inventing the wheel and there already exists some efficient code out...
12
5171
by: Serve Laurijssen | last post by:
Is code like the following allowed? I am talking about the comma after the last function in the initializer. void f(void) {puts("f");} void g(void) {puts("g");} struct Funcs { void (*f)(void); }; struct Funcs funcs = {
9
4515
by: Wayne | last post by:
I have the following string: "smith", "Joe", "West Palm Beach, Fl." I need to split this string based on the commas, but as you see the city state contains a comma. String.split will spilt the city state. Is there a built in function that I'm missing that will do the split and recognize that the comma in city state is part of the item? --
4
8477
by: =?Utf-8?B?RXJpY2E=?= | last post by:
I am trying to dynamically create a javascript link. But, I get the following error: BC32017: Comma, ')', or a valid expression continuation expected. Here is the line I try creating the link. I'm done staring at it. Can someone else see what may be the matter. Fresh pair of eyes maybe? Thanks: <a href='javascript:PopIt(<%# chr(39)
2
1577
by: Arnold | last post by:
Hi there, In a textbox containing a string of ID numbers each separated by a comma, how can I keep only the first value, and then remove all of the other numbers and commas behind it? The first value may contain 1 or 2 or more characters. Thanks much, Eric
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10384
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10130
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7667
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5553
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4338
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.