在PHP preg_replace函数中包括换行 [英] Including new lines in PHP preg_replace function

查看:172
本文介绍了在PHP preg_replace函数中包括换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试匹配可能出现在多行上的字符串.它以特定的字符串开头和结尾:

I'm trying to match a string that may appear over multiple lines. It starts and ends with a specific string:

{a}some string
can be multiple lines
{/a}

我可以用正则表达式获取{a}{/a}之间的所有内容吗?看来.与换行符不匹配,但是我尝试了以下操作而没有运气:

Can I grab everything between {a} and {/a} with a regex? It seems the . doesn't match new lines, but I've tried the following with no luck:

$template = preg_replace( $'/\{a\}([.\n]+)\{\/a\}/', 'X', $template, -1, $count );
echo $count; // prints 0

它匹配.或\ n独自一人,但不在一起时!

It matches . or \n when they're on their own, but not together!

推荐答案

使用 s修饰符:

$template = preg_replace( $'/\{a\}([.\n]+)\{\/a\}/s', 'X', $template, -1, $count );
//                                                ^
echo $count;

这篇关于在PHP preg_replace函数中包括换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆