我有YAML Front Matter,我想用PHP解析:
---
title = A nice title goes here
tags = tag1 tag2 tag3
---
This is the content of this entry...
Line2
Line3
Run Code Online (Sandbox Code Playgroud)
我知道它是关于某种类型的Ruby gem,但我想在PHP中使用它来创建一个用户友好的flatfile博客引擎.
我还有一个名为Phrozn的项目片段.也许为了帮助我尽可能地解决问题,你们可以方便地看到它.
private function parse()
{
if (isset($this->template, $this->frontMatter)) {
return $this;
}
$source = $this->readSourceFile();
$parts = preg_split('/[\n]*[-]{3}[\n]/', $source, 2);
if (count($parts) === 2) {
$this->frontMatter = Yaml::load($parts[0]);
$this->template = trim($parts[1]);
} else {
$this->frontMatter = null;
$this->template = trim($source);
}
return $this;
}
Run Code Online (Sandbox Code Playgroud)