How to make this Regex more flexible?
What to you thing about:

&lt;?=label ?&gt;  OR:
&lt;?=function(arg1, arg2, arg3) ?&gt;


PHP is already a template engine

Just write a wrapper around a include call and your template system is ready to use and more flexible than anything you could write.



That said, why to you match single lines? Please have a look at preg_replace_callback.

/\&lt;\%\s*(.+?)\s*\%\&gt;/


If you want to put the whitespace trimming in the callback and as you don't need to escape &lt;&gt;%, you could even use

/&lt;%(.+?)%&gt;/