Results 1 to 10 of 10
  1. #1
    Affiliate Marketer Rogi's Avatar
    Join Date
    January 18th, 2005
    Location
    Melbourne
    Posts
    415
    Splitting the Fields in Overstock Feed
    Hello Everyone,

    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!

    Igor

  2. #2
    Affiliate Marketer Rogi's Avatar
    Join Date
    January 18th, 2005
    Location
    Melbourne
    Posts
    415
    ~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

  3. #3
    Newbie
    Join Date
    May 11th, 2005
    Posts
    8
    undated explode answer
    hum need to think about this... read the question first!!

    Crispin
    Last edited by crispin; September 2nd, 2005 at 01:12 AM. Reason: not a useful answer

  4. #4
    Affiliate Marketer Rogi's Avatar
    Join Date
    January 18th, 2005
    Location
    Melbourne
    Posts
    415
    [no need for this... previous post edited]

  5. #5
    Newbie
    Join Date
    May 11th, 2005
    Posts
    8
    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++)
    {
    if(preg_match('|"(.+)"|',$array[$i]))
    {
    $result[$k++] = str_replace('"','',$array[$i]);
    }
    elseif(preg_match('|"(.+)|',$array[$i]))
    {
    $result[$k++] = str_replace('"','',$array[$i++].','.$array[$i]);
    }
    else
    {
    $result[$k++] = $array[$i];
    }
    }

    print_r($result);

    I'm not really a programmer, but I can get the result I want eventually.

    Crispin

  6. #6
    Moderator MichaelColey's Avatar
    Join Date
    January 18th, 2005
    Location
    Mansfield, TX
    Posts
    16,232
    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.
    Michael Coley
    Amazing-Bargains.com
     Affiliate Tips | Merchant Best Practices | Affiliate Friendly? | Couponing | CPA Networks? | ABW Tips | Activating Affiliates
    "Education is the most powerful weapon which you can use to change the world." Nelson Mandela

  7. #7
    Full Member
    Join Date
    January 18th, 2005
    Posts
    396
    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)

    Charles

  8. #8
    Affiliate Marketer Rogi's Avatar
    Join Date
    January 18th, 2005
    Location
    Melbourne
    Posts
    415
    Hello all,

    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.

  9. #9
    Affiliate Marketer Rogi's Avatar
    Join Date
    January 18th, 2005
    Location
    Melbourne
    Posts
    415
    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.

  10. #10
    Moderator MichaelColey's Avatar
    Join Date
    January 18th, 2005
    Location
    Mansfield, TX
    Posts
    16,232
    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").

    http://us2.php.net/fgetcsv

    http://www.creativyst.com/Doc/Articl...htm#FileFormat
    Michael Coley
    Amazing-Bargains.com
     Affiliate Tips | Merchant Best Practices | Affiliate Friendly? | Couponing | CPA Networks? | ABW Tips | Activating Affiliates
    "Education is the most powerful weapon which you can use to change the world." Nelson Mandela

  11. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. Replies: 1
    Last Post: February 9th, 2005, 12:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •