我需要一种人类可读且可解析的文档格式

发布于 2024-08-28 05:27:32 字数 649 浏览 12 评论 0原文

我正在从事其中一个项目,其中有一百万种更好的方法来完成我所需要的,但我别无选择,我必须这样做。它是这样的:

有一个网络表单,当用户填写并点击提交时,将使用表单数据创建人类可读的文本文件。它看起来像这样:

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

我的问题是这样的:我需要将此文本文件解析回 Web 表单,以便用户可以编辑它。

我怎样才能以万无一失的方式完成这个任务呢?数据库不是一个选项,我必须使用这些文本文件。

我的问题

  • 是否有一种万无一失的方法可以使用上面示例中的格式来执行此操作?
  • 什么人类可读格式会更好(换句话说,我可以更改格式)
  • 人类可读意味着非程序员可以阅读它并知道什么是什么。

该项目使用PHP。

更新

通过人类可读,我的意思是任何人都可以阅读文本并且不会被它淹没,包括你的祖母。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

玩心态 2024-09-04 05:27:32

我需要一个人类可读的,但是
可解析的文档格式

这就是 YAML 的设计目的。您可以在他们的网站或维基百科上阅读更多相关信息。

引用维基百科:

YAML 语法被设计为很容易
映射到大多数人常见的数据类型
高级语言:列表、散列和
标量。它熟悉的缩进
轮廓和精干的外观使它
特别适合以下任务
人类可能会查看或编辑数据
结构,例如配置
文件,调试期间转储,以及
文档标题

相对于 XML 的优点是它不使用可能使用户感到困惑的标签。我认为它比 INI(也提到过)更干净,因为它只是使用冒号而不是等号、分号和引号。

示例 YAML 如下所示:

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.

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.
不即不离 2024-09-04 05:27:32

I'd say either use

or just about any lightweight markup language you deem appropriate.

千鲤 2024-09-04 05:27:32

您可能想查看 YAML

http://www.yaml.org/

我同意 Pablo Fernandez 的回复。我认为 JSON 可能也是一个不错的选择。

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.

童话里做英雄 2024-09-04 05:27:32

XML 是一个选项。

XML is an option.

邮友 2024-09-04 05:27:32

我只是想说 INI 字符串非常可读:

Pet_Name = "Fred"

但是,您始终可以采用自己的格式。基本上

Key: ValueValueValueValueValueValue
Key: ValueValue

,您将通过换行符分解字符串,查找冒号前面的文本字符串并将其用作键,冒号之后和换行符之前的数据是值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文