的我用這個模式:PHP的正則表達式的任何字符
'/({for) (\d+) (times})([\w+\d+{}]{0,})({endfor})/i'
轉換
{for 3 times}App2{endfor}
在
App2 App2 App2
但這不工作:
{for 7 times}
App2
{endfor}
這是我很小的模板引擎的一小部分。
這只是爲了好玩
$mactos = Array(
'/({for) (\d+) (times})([\w+\d+{}]{0,})({endfor})/i' => '<?php for($i=0;$i<${2};$i++) : ?> ${4} <?php endfor; ?' . '>',
'/({{)(\w+)(}})/i' => '<?php echo $${2}; ?' . '>'
);
$php = file_get_contents('teamplate.php');
foreach ($this->getPatternAndReplacement() as $pattern => $replacement) {
$php = preg_replace($pattern, $replacement, $php);
}
我讀過,(...)捕捉到任何但是
'/({for) (\d+) (times})(...)({endfor})/i'
不起作用=(
邊注:大多數的元字符失去字符類中的特殊含義,所以你讓文字'+'字符。假設你不想接受字面值'+'字符,'[\ w + \ d + {}] {0,}'似乎相當於'[\ w {}] *'(因爲'*' 0,}'和'\ d'被包含在'\ w'中。 – Wiseguy