Skip to content

Instantly share code, notes, and snippets.

@greenbicycle
Last active April 5, 2016 02:38
Show Gist options
  • Save greenbicycle/af77232b738b00bf7adbe225e1fbdad2 to your computer and use it in GitHub Desktop.
Save greenbicycle/af77232b738b00bf7adbe225e1fbdad2 to your computer and use it in GitHub Desktop.
Parse yaml section from Markdown file
#!/usr/bin/env php
<?php
/*
* I like to use Markdown with embedded yaml to store certain info
* This is a quick and dirty first attempt. I probably will find some
* other chars to add to the preg_match_all pattern.
*
*/
$file=$argv[1];
if(file_exists($file)) {
$input = file_get_contents($file);
preg_match_all('|```yaml\n([a-z:\n!\- ./@]*)```|', $input, $matches);
var_dump(yaml_parse($matches[1][0]));
} else {
print "Hang your head in shame. That file does not exist.";
}
/*
This will work on files that are formatted like this:
Lorem ipsum, lorem ipmore...
```yaml
breakfast: yaml for breakfast
lunch: yaml for lunch
dinner: yaml for dinner
```
Morem lorem....
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment