In PHP, the challenge of matching r and n without using [rn] requires a different approach.
PCRE provides several escape sequences for handling newlines, including R.
Example:
<code class="php">$string = " Test "; // Match any Unicode newline sequence if (preg_match('~\R~u', $string)) { echo "Matched CR, LF, or any Unicode newline."; }</code>
PCRE supports five conventions for indicating line breaks in strings:
Using (*ANYCRLF) ensures that only CR, LF, or CRLF is matched.
<code class="php">if (preg_match('~(*ANYCRLF)\R~', $string)) { echo "Matched CR, LF, or CRLF."; }</code>
Within character classes, R loses its special meaning and is treated as the literal "R" character.
The above is the detailed content of How to Match \\r and \\n in PHP Without Using [\\r\\n]?. For more information, please follow other related articles on the PHP Chinese website!