Matching multi-line text

时间:2015-06-15 14:34:53

标签: php regex

I have the following text:

[FONT=arial, helvetica]this

is a multiline text

[/FONT]

[FONT=arial, helvetica] this is a single line comment[/FONT]

I'm trying to match the text inside the bbcode tags with this pattern:

\[FONT=(.*?)\](.*?)\[\/FONT\]\im

But it only matches the single line text, why? I've even added the multi-line flag.

http://www.regexr.com/3b72f

2 个答案:

答案 0 :(得分:3)

Instead of multiline flag you need single-line here.

/\[FONT=(.*?)\](.*?)\[\/FONT\]/isg

Single-line modifier makes dot symbol match newline characters as well - it is exactly your issue.

Multiline modifier only makes beginning/end characters (^ and $) match start/end of line.

答案 1 :(得分:2)

\[FONT=([\s\S]*?)\]([\s\S]*?)\[\/FONT\]

You can also use this without any flags.See demo.

https://regex101.com/r/hI0qP0/28