本文介绍了任何类型字符的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); }我读过 (...) 捕捉任何东西,但与
I've read that (...) catch anything but with
'/({for) (\d+) (times})(...)({endfor})/i'不起作用 =(.
推荐答案如果您的字面意思是 (...),那就是正好匹配三个字符的组.(.+) 将匹配一个或多个任何字符,除了...
If you literally mean (...), that is a group that matches exactly three characters. (.+) would match one or more of any characters, except...
默认情况下,. 匹配任何除了换行符.
By default, . matches anything except newlines.
s (PCRE_DOTALL)如果设置了此修饰符,则模式中的点元字符将匹配所有字符,包括换行符.没有它,将排除换行符.
s (PCRE_DOTALL) If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded.
使用 s 修饰符 允许 . 匹配换行符.
Use the s modifier to allow . to match newline characters.
/your pattern/s示例(还有此处):
$str = <<<STR {for 7 times} App2 {endfor} STR; preg_match('/({for) (\d+) (times})(.+)({endfor})/s', $str, $matchParts); print_r($matchParts); OUTPUT: Array ( [0] => {for 7 times} App2 {endfor} [1] => {for [2] => 7 [3] => times} [4] => App2 [5] => {endfor} )更多推荐
任何类型字符的php正则表达式
发布评论