I've got a regular expression that I've been mulling over for the past couple days and was hoping for some insight from someone with a better understanding of regular expressions. I've posted some sample code below for a better idea of what it is I'm looking for. I want to say thank you beforehand to all those who reply to this thread.
//Example 1.
//Both conditionals sections are top level, in that they aren't nested
$tpl = '<p>
<!-- if: time_of_day > 1200 -->
Good afternoon,
<!-- else: time_of_day -->
Good morning...
<!-- end_if: time_of_day -->
<!-- if: logged_in -->
Mr. Smith
<!-- else: logged_in -->
esteemed guest
<!-- end_if: logged_in -->
</p>';
//You'll notice two matches are returned
preg_match_all('/<!-- if: ([\w]+) (?:([<>=]) (\w+) )?-->((?:(?!<!-- else: \1 -->).)*)(?:<!-- else: \1 -->(.*))?<!-- end_if: \1 -->/s', $tpl, $arr);
print_r($arr);
//Example 2.
//One conditional section nested within the other
$tpl = '<p>
<!-- if: time_of_day > 1200 -->
Good afternoon,
<!-- if: logged_in -->
Mr. Smith
<!-- else: logged_in -->
esteemed guest
<!-- end_if: logged_in -->
<!-- else: time_of_day -->
Good morning...
<!-- end_if: time_of_day -->
</p>';
//You'll notice only match is returned in this instance
preg_match_all('/<!-- if: ([\w]+) (?:([<>=]) (\w+) )?-->((?:(?!<!-- else: \1 -->).)*)(?:<!-- else: \1 -->(.*))?<!-- end_if: \1 -->/s', $tpl, $arr);
print_r($arr);
Ideally what I'd like is the match returned in example 2 to be similar to the matches returned in example 1. My current regular expression is negating matches if they are nested within another match.