php-regexHow to use PHP regex to match a multiline?
Using PHP regex to match a multiline is possible with the /m
modifier. This modifier allows the pattern to match across multiple lines.
Example code
$string = "This is a
multiline string";
$pattern = "/This.*string/m";
if (preg_match($pattern, $string)) {
echo "Match found!";
}
Output example
Match found!
Code explanation
$string
: This is the string that will be searched for a match.$pattern
: This is the regular expression pattern that will be used to search for a match.preg_match()
: This is the PHP function used to search for a match. It takes two parameters, the regular expression pattern and the string to search./m
: This is the modifier used to allow the pattern to match across multiple lines.
Helpful links
More of Php Regex
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match an exact string?
- How to use PHP regex to match an XML tag?
- How to use PHP regex to match whitespace?
- How to use PHP regex to match URL path?
- How to use PHP regex to match a zip code?
- How to use PHP regex with zero or more occurrences?
- How to match a double quote in PHP regex?
- How to match the end of a string when using regex in PHP?
- How to match a single quote in PHP regex?
See more codes...