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

标签 php parsing text-files human-readable

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

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

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

我的问题是:我需要将此文本文件解析回网络表单,以便用户可以对其进行编辑。

我怎样才能以万无一失的方式实现这一目标?数据库不是一个选项,我必须使用这些文本文件。

我的问题:

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

这个项目使用 PHP。

更新

我所说的人类可读是指任何人都可以阅读文本而不会被它淹没,包括你的祖母。

最佳答案

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

这就是 YAML被设计成。您可以在他们的网站或 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

与 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.

关于php - 我需要一种人类可读但可解析的文档格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2595913/

相关文章:

android - 不从资源解析布局并在运行时设置它

javascript - 为什么要从头到尾对数组进行切片?

java - 在 Java 中一次读取两行文本文件的最佳方法是什么?

php - 使用 php 和 html 从我服务器上的目录填充 'select list'

php - 允许用户将 "Tab-delimited"数据粘贴到文本区域中

php - 将 WAMP 与 LDAP 结合使用时出错

javascript - VM[number] 文件导致未捕获的语法错误 : Unexpected identifier

reactjs - 如何使用 React.js 创建和更新文本文件?

python - 过滤元组列表以删除所有奇数或总和小于 80 的列表

php - 在 apache 上将不同的网站分配给不同的网络