Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP preg_match (.*) not matching past line breaks [duplicate]

Tags:

php

preg-match

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

Paragraph one
Paragraph two
Paragraph three
Paragraph four

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

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?

like image 726
Norse Avatar asked Oct 01 '12 03:10

Norse


3 Answers

Use the s modifier.

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

Pattern Modifiers

like image 87
Tasso Evangelista Avatar answered Oct 22 '22 06:10

Tasso Evangelista


Add the multi-line modifier.

Eg:

preg_match('/Para(.*)three/m', $row['file'], $m)
like image 28
temporalslide Avatar answered Oct 22 '22 05:10

temporalslide


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

preg_match('/Para(.*)three/s', $row['file'], $m);
like image 4
Jen Avatar answered Oct 22 '22 06:10

Jen