Regex replace在PHP中创建模板
我有一个字符串,看起来像这样:Regex replace在PHP中创建模板,php,regex,Php,Regex,我有一个字符串,看起来像这样: {{imagename.jpg|left|The caption for this image. Includes a variety of chars}} <p>Some text, lots of paragraphs here.</p> {{anotherimage.jpg|right|Another caption.}} 谁能帮我一下正则表达式,这样我就可以通过print\r运行匹配了吗?您错过了*(或者,可能是+)量词。您的原始
{{imagename.jpg|left|The caption for this image. Includes a variety of chars}}
<p>Some text, lots of paragraphs here.</p>
{{anotherimage.jpg|right|Another caption.}}
谁能帮我一下正则表达式,这样我就可以通过
print\r
运行匹配了吗?您错过了*
(或者,可能是+
)量词。您的原始表达式将只匹配一个非-}
字符
$string = preg_replace_callback('!\{\{([^}]*)\}\}!', 'template_function', $string);
还修改了打印($matches[1])代码>这样就可以打印出实际的匹配项。所以,我错过了流血*!我知道我一定快到了!这就是堆栈溢出如此可怕的原因——让其他人浏览您的代码非常有用。
$string = preg_replace_callback('!\{\{([^}]*)\}\}!', 'template_function', $string);
function template_function($matches) {
print_r($matches[1]);
}
function parse_images($string) {
$string = preg_replace_callback('/\{\{([^}]*)\}\}/', 'template_function', $string);
return $string;
}