php-regexHow to use PHP regex to match a line break?
To match a line break in PHP using regex, use the \R
character class. This character class matches any line break sequence, including \r
, \n
, and \r\n
.
Example code
$string = "This is a string
with a line break";
if (preg_match('/\R/', $string)) {
echo "Line break found!";
}
Output example
Line break found!
Code explanation
\R
: The character class that matches any line break sequence.preg_match()
: The function used to match a regular expression against a string.
Helpful links
More of Php Regex
- How to use PHP regex to match an exact string?
- How to use PHP regex with zero or more occurrences?
- How to use PHP regex to match a nbsp HTML whitespace?
- How to use PHP regex to match a zip code?
- How to use PHP regex to get a YouTube video ID?
- How to match a space using PHP regex?
- How to use PHP regex with the "x" modifier?
- How to use PHP regex to match a year?
- How to use PHP regex to match UUID?
- How to use the "s" modifier in PHP regex?
See more codes...