Skip to content

Instantly share code, notes, and snippets.

@logarytm
Last active July 22, 2017 11:26
Show Gist options
  • Select an option

  • Save logarytm/a5c7b4ebd8f166fb8884 to your computer and use it in GitHub Desktop.

Select an option

Save logarytm/a5c7b4ebd8f166fb8884 to your computer and use it in GitHub Desktop.
preprocessor for arbitrary text
<?php
$config = ...;
$input = file_get_contents('php://stdin');
$output = $input;
$opening = '<%';
$closing = '%>';
for ($offset = strpos($output, $opening); $offset !== false; $offset = strpos($output, $opening)) {
$endOffset = strpos($output, $closing, $offset);
$expression = substr($output, $offset + strlen($opening), $endOffset - $offset - strlen($opening));
$expression = trim($expression);
$output = substr_replace($output, evaluate($expression, $config), $offset, $endOffset - $offset + 2);
}
function evaluate($expression, $config)
{
$value = '';
$command = $expression[0];
$inner = trim(substr($expression, 1));
if ($command === '=') {
$chunks = explode('.', $inner);
$value = $config;
foreach ($chunks as $chunk) {
$value = $value[$chunk];
}
} else if ($command === '.') {
return file_get_contents($inner);
}
return $value;
}
echo $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment