views:

250

answers:

5

I'm working on one of those projects where there are a million better ways to accomplish what I need but I have no choice and I have to do it this way. Here it is:

There is a web form, when the user fills it out and hits a submit a human readable text file is created using the form data. It looks like this:

field_1: value for field one

field_2: value for field two
more data for field two (field two has a newline in it!)

field3: some more data

My problem is this: I need to parse this text file back into the web form so that the user can edit it.

How could I, in a foolproof way, accomplish this? A database is not an option, I have to use these text files.

My Questions:

  • Is there a foolproof way to do this using the format in the example above?
  • What human readable format would work better (in other words I can change the format)
  • Human readable means that a non programmer could read it and know what is what.

This project uses PHP.

UPDATE

By human readable I mean that anyone could read the text and not be overwhelmed by it, including your grandmother.

A: 

I'm just gonna say that an INI string is pretty readable:

Pet_Name = "Fred"

But, you could always roll your own format. Something like:

Key: ValueValueValueValueValueValue
Key: ValueValue

Basically, you would explode the string by newlines, look for text strings infront of colons and use that as the key, and the data after the colon and before the newline is the value.

Chacha102
Thats what I'm doing now, but I've ran into some parsing problems where the data included text that messed up the convention like a new line followed by a word and a colon. Not foolproof enough.
macinjosh
+19  A: 

I Need a Human Readable, Yet Parse-able Document Format

This is what YAML was designed to be. You can read more about it on their site or on Wikipedia.

To quote Wikipedia:

YAML syntax was designed to be easily mapped to data types common to most high-level languages: list, hash, and scalar. Its familiar indented outline and lean appearance makes it especially suited for tasks where humans are likely to view or edit data structures, such as configuration files, dumping during debugging, and document headers

The advantage over XML is that it doesn't use tags which might confuse users. And I think it's cleaner than INI (which was also mentioned) because it simply uses colons instead of equals signs, semicolons and quotes.

Sample YAML looks like:

invoice: 34843
date   : 2001-01-23
bill-to: &id001
    given  : Chris
    family : Dumars
    address:
        lines: |
            458 Walkman Dr.
            Suite #292
        city    : Royal Oak
        state   : MI
        postal  : 48046
ship-to: *id001
product:
    - sku         : BL394D
      quantity    : 4
      description : Basketball
      price       : 450.00
    - sku         : BL4438H
      quantity    : 1
      description : Super Hoop
      price       : 2392.00
tax  : 251.42
total: 4443.52
comments: >
    Late afternoon is best.
    Backup contact is Nancy
    Billsmer @ 338-4338.
Josh
Oh, and there's at least two PHP libraries for reading/writing YAML
Josh
http://stackoverflow.com/questions/294355/php-yaml-parsers
Josh
Great answer for a great question. I didn't know about YAML.
Leniel Macaferi
I learned about it when I learned Ruby
Josh
I called my grandmother about this. She's totally cool with it.
webbiedave
Funny... my grandmother said "YAM-What?" ;-)
Josh
+1  A: 

XML is an option.

Leniel Macaferi
Wasn't human-readable one of the requirements?
Roger Pate
+4  A: 

You might want to look into YAML

http://www.yaml.org/

I agree with Pablo Fernandez response. I think JSON might be a good choice as well.

Frank Hale
+7  A: 

I'd say either use

or just about any lightweight markup language you deem appropriate.

Gordon
Why was this voted down? This provided some good options!
Josh
Yeah, who is mass downvoting here? ... Sorry, I'm out of votes for today so I can't counter.
Pekka
LOL @Gordon I was just wondering that myself. I voted this *up* because I think it contains at least 2 great suggestions.
Josh
I just voted it up. I was also wondering what is going on with all the down votes.
Frank Hale