0

I have a little problem with parsing a MediaWiki Template-Block: http://regex101.com/r/dD1hC9/1

  • The RegEx I use to get the template from the text is: \{\{Template1 (.+?)\}\}.
  • The template: {{Template1 |Parameter1=Text |Parameter2=Text |Parameter3={{Template2|hier|steht|text}} |Parameter4=Text }}

Is there any possibility, to parse the complete block in an associative array (parametername => value), without loosing the template in Parameter3 respectively in all parameters?

Nemo
  • 2,441
  • 2
  • 29
  • 63

1 Answers1

0
<?php
$subject = "{{Template1 |Parameter1=Text |Parameter2=Text |Parameter3={{Template2|hier|steht|text}} |Parameter4=Text }}";
preg_match('/{{Template1(.*)}}/', $subject, $matches);
preg_match_all('/ \|(\w+)=({{.*?}}|\w*)/', $matches[1], $matches);
$a = array_combine($matches[1], $matches[2]);
print_r($a);
?>

prints

Array
(
    [Parameter1] => Text
    [Parameter2] => Text
    [Parameter3] => {{Template2|hier|steht|text}}
    [Parameter4] => Text
)
Armali
  • 18,255
  • 14
  • 57
  • 171
  • This works well if you have only one template in the string, but not if you have multiple templates, alas. –  Mar 01 '17 at 16:33