我需要一种人类可读且可解析的文档格式
我正在从事其中一个项目,其中有一百万种更好的方法来完成我所需要的,但我别无选择,我必须这样做。它是这样的:
有一个网络表单,当用户填写并点击提交时,将使用表单数据创建人类可读的文本文件。它看起来像这样:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这就是 YAML 的设计目的。您可以在他们的网站或维基百科上阅读更多相关信息。
引用维基百科:
相对于 XML 的优点是它不使用可能使用户感到困惑的标签。我认为它比 INI(也提到过)更干净,因为它只是使用冒号而不是等号、分号和引号。
示例 YAML 如下所示:
This is what YAML was designed to be. You can read more about it on their site or on Wikipedia.
To quote Wikipedia:
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:
我想说要么使用
或任何您认为合适的轻量级标记语言。
I'd say either use
or just about any lightweight markup language you deem appropriate.
您可能想查看 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.
XML 是一个选项。
XML is an option.
我只是想说 INI 字符串非常可读:
但是,您始终可以采用自己的格式。基本上
,您将通过换行符分解字符串,查找冒号前面的文本字符串并将其用作键,冒号之后和换行符之前的数据是值。
I'm just gonna say that an INI string is pretty readable:
But, you could always roll your own format. Something like:
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.