php-regexHow to use regex in PHP to match any character including newline?
Regex (Regular Expressions) is a powerful tool used to match patterns in strings. In PHP, it can be used to match any character including newline.
Example code
$string = "This is a string
with a newline";
$pattern = "/^.*$/m";
if (preg_match($pattern, $string)) {
echo "Match found!";
}
Output example
Match found!
Code explanation
$string
: This is the string that we are trying to match.$pattern
: This is the regex pattern that we are using to match the string. The/m
modifier at the end of the pattern tells PHP to match the pattern across multiple lines.preg_match()
: This is the PHP function used to match the pattern against the string.
Helpful links
More of Php Regex
- How to use PHP regex to match a zip code?
- How to use PHP regex with zero or more occurrences?
- How to use PHP regex to get a YouTube video ID?
- How to use PHP regex to match a year?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match whitespace?
- How to use PHP regex to match tab?
- How to remove non-printable characters using PHP regex?
- How to use PHP regex to match a URL in a href attribute?
- How to use PHP regex in a case insensitive mode?
See more codes...