I have a template class to handle templates so, this is a line taken from that class. I'll spare many of the details, as many of them have to do with loading templates.
$tmpl = <<<tem
<div id="header2">
<a rel="home" href="//localhost/index.php">
<h1>Test Site</h1>
</a>
</div>
<nav>
<div class="menu">
<ul>
<li class="base">
<a rel="home" href="//localhost/index.php">Home</a>
</li>
<li><a href="//localhost/search.php">Search</a></li>
{if(\$is_guest)}
<li><a href="//localhost/member.php?view=register">Register</a></li>
<li><a href="//localhost/member.php?view=login">Login</a></li>
{/if}{else}
<li><a href="//localhost/profile.php?uid=1">Profile</a></li>
<li><a href="//localhost/usercp.php">My Account</a></li>
<li><a href="//localhost/member.php?view=logout">Logout</a></li>
{/else}
{if(\$is_admin)}
<li><a href="//localhost/admin">Admin</a></li>
{/if}
{!nav}
</ul>
<div class="floater"></div>
</div>
</nav>
tem;
$tmpl = preg_replace_callback('#{\$(.+?)}#u',array(get_class($this),'varsub'),$tmpl);
$tmpl = preg_replace_callback("#{if\(([A-Za-z0-9\$_!]+)\)}((?:(?R)|.*?)+)\{/if\}\{else\}((?:(?R)|.*?)+)\{/else\}#ismu",array(get_class($this),'boolelsesub'), $tmpl);
For some reason, this Regex results in the page never loading, instead of doing what I would expect. I've looked at many online resources however, none of them seem to mention using two recursive patterns at once.
It doesn't seem to be reaching the callback which I have assigned to it either.
protected function boolelsesub($input)
{
var_dump($input);
$h = fopen('bah.txt', 'a');
fwrite($h,'test');
fclose($h);
exit;
}
Edit: The contents of boolelsesub, which has been stripped down to make it easier to debug.
Try with this pattern:
$pattern = <<<'LOD'
~
{ if \( ([^)]+) \) }
( (?>[^{]+|(?R))* )
{ / if }
(?:
{ else }
( (?>[^{]+|(?R))* )
{ / else }
)?
~ix
LOD;
Even if your pattern works with this edit, the result will be inefficient due to (?:(?R)|.*?)+
. This part is inefficient in particular because .*?
is lazy and can match the empty string. To solve this, I replace it with a negated character class with a greedy quantifier [^{]+