PHP preg_match (.*) 不匹配过去的换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12667369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:00:21  来源:igfitidea点击:

PHP preg_match (.*) not matching past line breaks

phppreg-match

提问by Norse

I have this data in a LONGTEXT column (so the line breaks are retained):

我在 LONGTEXT 列中有此数据(因此保留换行符):

Paragraph one
Paragraph two
Paragraph three
Paragraph four

I'm trying to match paragraph 1 through 3. I'm using this code:

我正在尝试匹配第 1 段到第 3 段。我正在使用以下代码:

preg_match('/Para(.*)three/', $row['file'], $m);

This returns nothing. If I try to work just within the first line of the paragraph, by matching:

这不返回任何内容。如果我尝试仅在段落的第一行内工作,则通过匹配:

preg_match('/Para(.*)one/', $row['file'], $m);

Then the code works and I get the proper string returned. What am I doing wrong here?

然后代码工作,我得到正确的字符串返回。我在这里做错了什么?

回答by Tasso Evangelista

Use smodifier.

使用s修饰符。

preg_match('/Para(.*)three/s', $row['file'], $m);

http://php.net/manual/en/reference.pcre.pattern.modifiers.php

http://php.net/manual/en/reference.pcre.pattern.modifiers.php

回答by temporalslide

Add the multi-line modifier.

添加多行修饰符

Eg:

例如:

preg_match('/Para(.*)three/m', $row['file'], $m)

回答by Jen

Try setting the regex to dot-all (the extra 's' parameter at the end), so it includes line breaks:

尝试将正则表达式设置为 dot-all(末尾的额外 's' 参数),因此它包括换行符:

preg_match('/Para(.*)three/s', $row['file'], $m);

回答by Danon

If you don't like /at the start and and, use T-Regx

如果你不喜欢/一开始和和,使用T-Regx

$m = Pattern::of('Para(.*)three')->match($row['file'])->first();