In your case, it's better to use preg_match with its additional parameter and parenthesis:
preg_match("#((?:<|<)%)([\s]*(?:[^ø]*)[\s]*?)(%(?:>|>))#i",$markup, $out);
print_r($out);
Array
(
[0] => <% your stuff %>
[1] => <%
[2] => your stuff
[3] => %>
)
By the way, check this online tool to debug PHP regexp, it's so useful !
http://regex.larsolavtorvik.com/
EDIT : I hacked the regexp a bit so it's faster. Tested it, it works :-)
Now let's explain all that stuff :
- preg_match will store everything he captures in the var passed as third param (here $out)
- if preg_match matches something, it will be store in $out[0]
- anything that is inside () but not (?:) in the pattern will be stored in $out
The patten in details :
#((?:<|<)%)([\s]*(?:[^ø]*)[\s]*?)(%(?:>|>))#i can be viewed as ((?:<|<)%) + ([\s]*(?:[^ø]*)[\s]*?) + (%(?:>|>)).
((?:<|<)%) is capturing < or < then %
(%(?:>|>)) is capturing % then < or >
([\s]*(?:[^ø]*)[\s]*?) means 0 or more spaces, then 0 or more times anything that is not the ø symbol, the 0 or more spaces.
Why do we use [^ø] instead of . ? It's because . is very time consuming, the regexp engine will check among all the existing characters. [^ø] just check if the char is not ø. Nobody uses ø, it's an international money symbol, but if you care, you can replace it by chr(7) wich is the shell bell char that's obviously will never be typed in a web page.
EDIT2 : I just read your edit about capturing all the matches. In that case, you´ll use preg_match_all the same way.