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 match a space using PHP regex?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match a zip code?
- How to use an "or" condition in PHP regex?
- How to use PHP regex to get a YouTube video ID?
- How to convert a PHP regex to JavaScript regex?
- How to use PHP regex with zero or more occurrences?
- How to use PHP regex to match a year?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match special characters?
See more codes...