I have a string that looks like this:
$sample = <<<EOD
lots of gibberish
before what I want{{Template
| x = 1
| y = 2|z= 3}}more gibberish
here too. {{Other|test}}
EOD;
I'd like to get rid of the gibberish and just get:
$sample = <<<EOD
{{Template
| x = 1
| y = 2|z= 3}}
EOD;
I'd normally look for "{{Template" and end with "}}", but my problem is, it might look like this sometimes:
$sample2 = <<<EOD
lots of gibberish
before what I want{{Template
| x = 1
| y = {{Convert|1|2}}|z= {{Convert|3|4}}}}more gibberish
here too. {{Other|test}}
EOD;
In this situation, it opens and closes some curley brackets, but I want it to match this:
$sample2 = <<<EOD
{{Template
| x = 1
| y = {{Convert|1|2}}|z= {{Convert|3|4}}}}
EOD;
How could I do this?
\{(.*)\}/s
That should match the outermost brackets, including newlines in between.
(\{\{Template(.*)\}\})\w+/s
This will match the {{Template start with a }} followed by some gibberish. The "template" part will be stored in $1
\{\{Template[^\{]*(?:\{\{[^\}]*\}\}[^\{]*)*?[^\{]*\}\}
See the demo here
Explain
\{\{Template //Starts with {{Template
[^\{]* //Any chars except {
(?:\{\{[^\}]*\}\}[^\{]*)*? //Any inner groups+trailing chars conditional
[^\}]* //Any chars except {
\}\} //Ends with }}