Okay, so you want to check that <# #> are properly paired. I'll assume they can't be nested <# like <# this #> at all #>...
They won't be properly paired if:
There's a #> before the first <#
There's a #> after the last <#
There's a #> ... #> or a <# ... <# where the "..." don't contain a <# or #>.
Test to make sure they're properly paired first. If they are, then it can be split into an array of $bits. If they aren't set $bits=array(). After that, you can see if the splitting was successful, because only then would count($bits)>0.
There are various ways of doing the test, but one preg_match() regexp (which
is basically a direct implementation of those rules) would probably[] be
/^(((?!<#).)*?)#>|<#(((?!#>).)*?)$|#>(((?!<#).)*?)#>|<#(((?!#>).)*?)<#/
if(preg_match(...that...,$val))
{ // Bad string.
$bits=array();
}
else
{ $bits=preg_split(...etc...);
}
Other approaches might be better, though.
[*]I mean, it's not like I tested it or anything...