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
PHP preg_match (.*) not matching past line breaks
提问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
回答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);

