Home > Backend Development > PHP Tutorial > How to Match \\r and \\n Without Using [\\r\\n]?

How to Match \\r and \\n Without Using [\\r\\n]?

Linda Hamilton
Release: 2024-11-04 07:40:02
Original
607 people have browsed it

How to Match \r and \n Without Using [\r\n]?

Matching r and n without [rn]

When attempting to match the newline characters r and n using v (vertical white space), one may encounter unexpected results. However, there are several alternatives to achieve this match.

Alternatives:

R (Unicode Newline Sequence):

  • By default, R matches Unicode newline sequences in the ASCII range.
  • Using the u (unicode) modifier extends the match to include newline characters outside the ASCII range.
  • For instance:
<code class="php">preg_match('~\R~', $string); // Matches any Unicode newline sequence in ASCII range
preg_match('~\R~u', $string); // Matches any Unicode newline sequence</code>
Copy after login

(*ANYCRLF) R:

  • This variation restricts R to match only CR, LF, or CRLF.
  • For example:
<code class="php">preg_match('~(*BSR_ANYCRLF)\R~', $string); // Matches only CR, LF, or CRLF</code>
Copy after login

The above is the detailed content of How to Match \\r and \\n Without Using [\\r\\n]?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template