with preg_replace:
$string = '{{ module:projects,id=1 }}';
$string = preg_replace('/{{ module:([^,]+),id=([0-9]+) }}/', 'include(\'$1.php?id=$2\')', $string);
// $string == include('projects.php?id=1')
Note: you can't actually include files with query strings on the end. PHP looks for file named 'projects.php?id=1'.
To get the file and actually include it:
preg_match_all('/{{ module:([^,]+),id=([0-9]+) }}/', $string, $matches);
foreach ($matches[1] as $key => $filename) {
// set $id before including the file. you could optionally set $_GET['id'].
$id = $matches[2][$key];
include($filename . '.php');
}
To support multiple parameters. Similar to Matmarbon's, but is more compact, doesn't assume parameter values will conform to \w, doesn't escape values, but also has less room for variations in spacing (/s*).
preg_match_all('/{{ module:([^,]+)((,[^=]+=[^,}]+)*) }}/', $string, $matches);
foreach ($matches[1] as $key => $filename) {
foreach (explode(',', trim($matches[2][$key], ', ')) as $vars) {
list($k, $v) = explode('=', $vars);
$_GET[$k] = $v;
}
// then this:
include($filename . '.php');
// or, to replace content:
ob_start();
include($filename . '.php');
$contents = ob_get_contents();
ob_end_clean();
$string = str_replace($matches[0][$key], $contents, $string);
}