Results 1 to 10 of 10
September 1st, 2005, 09:40 PM #1Splitting the Fields in Overstock Feed
Just trying to get the Overstock feed into my database and I'm having some problem with the fields.
The fields are separated by commas, and enclosed by quotation marks, except for the price field and MSRP field. This is where the problem comes in.
Can anyone help me out (let me know) how you would explode or separate into an array (in PHP) something like the following:
"Field 1", "Field 2", "This is, Field 3", 40, 50, "Field 6", "Field, comma 7"
I can't separate by comma as there are commas within the quotation marks, and i can't separate by quotation marks because some fields don't have them, etc.
How do you all handle this?
Thanks for all your help!
September 1st, 2005, 11:51 PM #2
~bump ~bump ~bump
Sorry to be impatient, it's the only thing i've got left to do today and i can't figure it out
September 2nd, 2005, 12:05 AM #3
undated explode answer
- Join Date
- May 11th, 2005
hum need to think about this... read the question first!!
Last edited by crispin; September 2nd, 2005 at 12:12 AM. Reason: not a useful answer
September 2nd, 2005, 12:17 AM #4
[no need for this... previous post edited]
September 2nd, 2005, 01:10 AM #5
- Join Date
- May 11th, 2005
Ok, this does work!
I'm sure there is an elegant way to do it using
preg_match_all or something like it, but this does the job.
(But it is a hack)
$datafeed_record = '"Field 1", "Field 2", "This is, Field 3", 40, 50, "Field 6", "Field, comma 7"';
$array = explode(',',$datafeed_record);
$k = 0;
for($i = 0;$i< sizeof($array);$i++)
$result[$k++] = str_replace('"','',$array[$i]);
$result[$k++] = str_replace('"','',$array[$i++].','.$array[$i]);
$result[$k++] = $array[$i];
I'm not really a programmer, but I can get the result I want eventually.
September 2nd, 2005, 09:47 AM #6
If Crispin's answer isn't enough, you might want to tell what language you're using. I use Perl, and the easiest way to handle commas embedded in CSV in Perl is to just use the "Text::CSV" module, which handles it automatically.
September 2nd, 2005, 11:47 AM #7
- Join Date
- January 17th, 2005
In php there is a function for comma separated variables CSV called fgetcvs() that will explode that datafeed into an array (split() in VB also will handle it the same way).
You may need a specialized function or an error handling routine to catch the few times that Overstock puts an extra quotation mark or two in the middle of a description (without a comma in between)
September 2nd, 2005, 12:05 PM #8
Crispin : Thanks for that Crispin, I'm just trying to get my head around what it's doing but seems like it might just work
MichaelColey : Using PHP for it, I think I mentioned it somewhere hidden in the first post, I probably should have used Perl, I'm better at it but just liked the whole PHP/MySQL set-up. It's a little easier, IMO anyway. Thanks for your help
micheck: Yeah i was googling around and found the fgetcsv function but not sure how it works with fields that are enclosed by quotation marks, as well as fields that are not -> in the same record.
Thanks for your help all. I'll get it sorted now.
September 2nd, 2005, 12:56 PM #9
Crispin, it's so close.... just one thing missing, but i should be able to sort it out.
Just if there are multiple comma's in a field, ie "Record, number, 4"
But I guess I can put a while loop in that elseif which searches for an end ".
Thanks a lot. Should work.
September 2nd, 2005, 01:59 PM #10
I would recommend using the function that PHP provides. There are several little "gotchas" in CVS that you might miss if you code it yourself. There can be embedded linefeeds in fields. Some fields may be quoted while others aren't. Fields that contain quotes will be quoted and the quotes will be converted to double quotes (for instance, "This field has ""quotes"" in it").
By Ginger in forum Cusimano.com ScriptsReplies: 1Last Post: February 9th, 2005, 11:49 AM