PHP Regular Expression does not match new line with s-modifier

I am trying to match a series of words that span two lines.

Say I have the following text:

this is a test
another line

My regex pattern using preg_match:

/test.*another/si

Test here: http://www.phpliveregex.com/p/2zj

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

All that I read indicates the use of the modifier "s" to include ".". to match newlines, but I can't get this to work. Any ideas?

+4
source share
3 answers

Your regular expression works correctly and perfectly on my local machine:

$input_line = "this is a test
another line";

preg_match("/test.*another/si", $input_line, $output_array);
var_dump($output_array);

:

array(1) {
  [0]=>
  string(13) "test
another"
}

, phpliveregex.com .

+3

:

/(?s)test.*another/i
+2

Yes, the modifier s, also known as the dotall modifier, causes the dot to .also match newlines.

Your regex is being used correctly, and this seems to work for me.

$text = <<<DATA
this is a test
another line
DATA;

preg_match('/test.*another/si', $text, $match);
echo $match[0];

See here demo.

Exit

test
another
+2
source

All Articles