Basically I'm writing a small template parser that uses regex to parse some tags, at the moment I'm having trouble with matching multiple tags in the same file.
Here is my current regex pattern:
$regex = '#\{if \$([A-Za-z0-9_]+)\}([^{]+)(\{else\})?([^{]+)?\{\/if\}#'
Am I missing something here?
Here an example of a template file I'm trying to parse
{if $name}
Hi my name is: {$name}.
{else}
No name set.
{/if}
{if $male}
Gender: Male.
{else}
Gender: Female.
{/if}
Here is my php code as it stands at the moment:
<?php
$tpl_file = file_get_contents('template.tpl');
$regex = '#\{if \$([A-Za-z0-9_]+)\}([^{]+)(\{else\})?([^{]+)?\{\/if\}#';
if (preg_match_all($regex, $tpl_file, $matches)) {
print_r($matches);
}
?>
Any help is greatly appreciated. Cheers