When I write a regex with .
in it, it doesn't match new lines.
preg_match('/.*+?/') ...
What do I need to write, to match all po开发者_运维技巧ssible characters, and new lines too?
Add the s
modifier, e.g.
'/./s'
By default .
will not match newlines. You can change this behaviour with the s
modifier.
The .
does not match new lines - and that is on purpose (though I am not really sure why). You would use the s
modifier to change this behaviour, and make .
match all characters, including the newline.
Example:
$text = "Foobar\n123"; // This is the text to match
preg_match('/^Foo.*\d+$/', $text); // This is not a match; the s flag isn't used
preg_match('/^Foo.*\d+$/s', $text); // This is a match, since the s flag is used
Apart from the s modifier you should think about using a negated character class. Instead of
#http://example\.org/.+/.+/.+#
you may use
#http://example\.org/[^/]+/[^/]+/[^/]+#
which ought to be faster.
Try this:
\r : carriage return
\n : new line
\w : even [a-zA-Z0-9_]
* : 0 or more. even : +?
$text = "a\nb\n123";
preg_match("/[\w\r\n]*/i", $text, $match);
print_r($match);
vide this list:
精彩评论