3
votes

I have a text that has multiple occurrences of variables in this format:

Example text will %%include%% these %%parameters%%

I'm trying to use this:

preg_match('/%%[\s\S]*?%%/i',$output, $matches);

But for some reason I'm getting only the first match, any ideas?

1
You don't need [\s\S] in PHP (as opposed to JavaScript). A simple . is enough when you set the /s option. Also, you don't need the /i option because there are no cased characters in your regex anyway: '/%%.*?%%/s' will do (together with preg_match_all()).Tim Pietzcker
@EmilVikström: Well, it's not really an answer to his question (which was about why he only got one match). But perhaps Pierpaolo could incorporate it into his answer.Tim Pietzcker

1 Answers

6
votes

Use preg_match_all because you need multiple matches.