任何类型字符的php正则表达式

编程入门 行业动态 更新时间:2024-10-27 14:24:13
本文介绍了任何类型字符的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正则表达式

本文发布于:2023-11-06 16:13:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564176.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符   类型   正则表达式   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!