<?php
$phpbb_code = '<!-- BEGIN navlinks -->
<span>{navlinks.U_VIEW_FORUM}{$MICRODATA}{navlinks.MICRODATA}{navlinks.FORUM_NAME}</span>
<!-- END navlinks -->';
function fix_begin_tokens($code, $parent_nodes = array())
{
// PHP 5.3 cannot use $this in an anonymous function, so use this as a work-around
$parent_class = $this;
$callback = function ($matches) use ($parent_class, $parent_nodes)
{
$hard_parents = explode('.', $matches[1]);
array_pop($hard_parents); // ends with .
if ($hard_parents)
{
$parent_nodes = array_merge($hard_parents, $parent_nodes);
}
$name = $matches[2];
$subset = trim(substr($matches[3], 1, -1)); // Remove parenthesis
$body = $matches[4];
// Replace <!-- BEGINELSE -->
$body = str_replace('<!-- BEGINELSE -->', '{% else %}', $body);
// Is the designer wanting to call another loop in a loop?
// <!-- BEGIN loop -->
// <!-- BEGIN !loop2 -->
// <!-- END !loop2 -->
// <!-- END loop -->
// 'loop2' is actually on the same nesting level as 'loop' you assign
// variables to it with template->assign_block_vars('loop2', array(...))
if (strpos($name, '!') === 0)
{
// Count the number if ! occurrences
$count = substr_count($name, '!');
for ($i = 0; $i < $count; $i++)
{
array_pop($parent_nodes);
$name = substr($name, 1);
}
}
// Remove all parent nodes, e.g. foo, bar from foo.bar.foobar.VAR
foreach ($parent_nodes as $node)
{
$body = preg_replace('#([^a-zA-Z0-9_])' . $node . '\.([a-zA-Z0-9_]+)\.#', '$1$2.', $body);
}
// Add current node to list of parent nodes for child nodes
$parent_nodes[] = $name;
// Recursive...fix any child nodes
$body = $parent_class->fix_begin_tokens($body, $parent_nodes);
// Need the parent variable name
array_pop($parent_nodes);
$parent = (!empty($parent_nodes)) ? end($parent_nodes) . '.' : '';
if ($subset !== '')
{
$subset = '|subset(' . $subset . ')';
}
$parent = ($parent) ?: 'loops.';
// Turn into a Twig for loop
return "{% for {$name} in {$parent}{$name}{$subset} %}{$body}{% endfor %}";
};
return preg_replace_callback('#<!-- BEGIN ((?:[a-zA-Z0-9_]+\.)*)([!a-zA-Z0-9_]+)(\([0-9,\-]+\))? -->(.+?)<!-- END \1\2 -->#s', $callback, $code);
}
$code = fix_begin_tokens($phpbb_code, '');
echo $code;
Output for git.master, git.master_jit, rfc.property-hooks
Fatal error: Uncaught Error: Using $this when not in object context in /in/vrAvM:10
Stack trace:
#0 /in/vrAvM(64): fix_begin_tokens('<!-- BEGIN navl...', '')
#1 {main}
thrown in /in/vrAvM on line 10
Process exited with code 255.
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.