Thread: Quoting Strings

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    1

    Quoting Strings

    I have data stored similar to:

    PHP Code:
    "lol data skadjs","ey how r u",hey,wowzor,"LOL HEY" 
    etc..

    Does anyone know a decent way to unquote the data without ruining it.

    Should come out like this:
    PHP Code:
    lol data skadjs
    ey how r u
    hey
    wowzor
    LOL HEY 
    Every method I've tried has failed (mainly due to one-words not getting quoted or commas in the actual data etc..

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What are you trying to do, in a bit more "helicopter view"?

    Most likely, you'll have to read the whole line in, and I can't think of a parser that will automatically respect quoted strings with commas and split the rest where there are commas, so you'll have to write your own parser. Not a very complex one, just keep track whether the text is in a quote or not. When you see a comma, you check if you are in quotes or not, and decide based on that if you need to start on the next string or not.

    --
    Mats

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It's a simple state machine. If you see a quote when you are not inside quotes, you change states to be inside quotes. If you see a comma and you are inside quotes, ignore it. If you see a quote and you are inside quotes, then you are no longer inside quotes. If you see a comma and you are not inside quotes, then that's the end of a token.

  4. #4
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    dont know if this help: first, run through the string to check if the amount of quotes is even, if not, return an error or something.
    Hello, testing testing. Everthing is running perfectly...for now

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by hdragon View Post
    dont know if this help: first, run through the string to check if the amount of quotes is even, if not, return an error or something.
    Alternatively, parse the string as you go alon, and if you hit end of line with the state "in quotes" then you issue an error - since either way you'll have to parse the string, you might just as well do the error checking in the parser, rather than first walk through the string, and then walk it again.

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM