The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

PHP Help

GrundlestiltskinGrundlestiltskin Behind you!Registered User regular
edited May 2009 in Help / Advice Forum
I'm trying to write a script to convert a csv into a tab delimited text file, but I'm running into trouble because some of the fields contain commas as regular punctuation.

I noticed that when the commas in the files in question were used as separators there were no spaces, and when they were used as punctuation there was always (as far as I can tell at first glance) the appropriate space after the comma. My thought was to replace all commas with tabs except commas followed by spaces, but I wasn't quite sure how to accomplish that. preg_replace doesn't seem to have a flag for exceptions to the replacement rule.

Thoughts?

3DS FC: 2079-6424-8577 | PSN: KaeruX65 | Steam: Karulytic | FFXIV: Wonder Boy
Grundlestiltskin on

Posts

  • clam2000clam2000 SeattleRegistered User regular
    edited May 2009
    So like:
    [PHP]
    preg_replace('/,([^ ])/',"\t\\1",$text);
    [/PHP]

    search for a comma followed by not a space, and replace it with a tab followed by the same character.

    clam2000 on
    cdogwal.gif
  • SeñorAmorSeñorAmor !!! Registered User regular
    edited May 2009
    Is there any way of making your initial delimiter (the comma) different? Say, a pipe or carat or something not normally used in standard typing?

    Edit:
    Clam's way should work too.

    SeñorAmor on
  • GrundlestiltskinGrundlestiltskin Behind you!Registered User regular
    edited May 2009
    Unfortunately the file is coming from a client and dealing with the format is part of my job, so I'm stuck with commas as a delimiter.

    Clam's method seems to mostly work, but it's creating a separate issue I can't really identify. Some fields are now starting with a comma (,N), not sure why.

    Edit: The above problem occurs when a field is empty - i.e, ",," appears in the file.

    Grundlestiltskin on
    3DS FC: 2079-6424-8577 | PSN: KaeruX65 | Steam: Karulytic | FFXIV: Wonder Boy
  • bowenbowen Sup? Registered User regular
    edited May 2009
    That's assuming the person doing the "normal typing" is following proper typing procedure. It's been my experience that normal users don't know proper typing procedures. That is to say, you'll see stuff like this "hi there,i like you, it`s hot out today,."

    Usually, with delimited files, text is required to be encased in quotes so that it's a tad bit easier to parse, otherwise it's going to be impossible.

    bowen on
    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • GrundlestiltskinGrundlestiltskin Behind you!Registered User regular
    edited May 2009
    Yeah this is an export from what I'm guessing is the client's shopping cart, but I'm not supposed to ask too many questions or they might get the impression that I don't know what I'm doing.

    Grundlestiltskin on
    3DS FC: 2079-6424-8577 | PSN: KaeruX65 | Steam: Karulytic | FFXIV: Wonder Boy
  • bowenbowen Sup? Registered User regular
    edited May 2009
    [PHP]

    $areWeInQuotes = false;

    foreach($text as &$char) {
    if($char == ',' && !$areWeInQuotes)
    $char = '\t';
    else if($char == '"')
    $areWeInQuotes = !$areWeInQuotes;
    }
    [/PHP]

    Something like this should work in PHP, if not exactly that. I haven't tested it, but you get the idea hopefully.

    Tell them it's virtually impossible unless the free text has been sanitized to use something else or quoted properly so it can be parsed and modified to the new format. Otherwise you're probably boned. (My code requires something like this [121,Frank,"Hi There, I'm Frank!"])

    bowen on
    not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
  • clam2000clam2000 SeattleRegistered User regular
    edited May 2009
    Just as a note, if you do have quotes for the fields then this is a csv document.

    PHP has support to deal with this format for you in version 5. Take a look at
    str_getcsv()

    clam2000 on
    cdogwal.gif
Sign In or Register to comment.