I would first like to say, are you creating your own template library? There are some mature ones around like Smarty which should be able to fill your requirements. This article by Jeff Atwood is also good read : "Don't Reinvent The Wheel, Unless You Plan on Learning More About Wheels"
If you want to roll your own solution then that is fine, here are some suggestions.
BBCode
This method relies on having the BBCode extension installed with PHP, it is native and does not rely on the regular expression engine (slow). This solution will probably be the fastest.
You can have the BBCode parser convert
[LOOP]<td><?php print $row->title; ?></td>[/LOOP]
into
<?php foreach($result as $row): ?><td><?php print $row->title; ?></td><?php endforeach; ?>
Look into the BBCode arguments to learn about passing variables into your [LOOP] syntax.
You can view some more examples on the bbcode_create method page.
Regular Expressions
They are a little bit slower, but are enabled by default on all PHP installations.
Credits to go Gumbo for this regular expression (You should up-vote him).
/\[LOOP]((?:[^[]+|\[(?=\/LOOP]))*)\[\/LOOP]/
You can then use it as follows.
if (preg_match('%\[LOOP\]((?:[^[]+|\[(?=/LOOP\]))*)\[/LOOP\]%i', $subject, $result)) {
# Successful match
# You can access the content using $result[1];
} else {
# Match attempt failed
}
-Mathew