Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6723299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:33:09+00:00 2026-05-26T09:33:09+00:00

I have a text which might have different newline styles. I want to replace

  • 0

I have a text which might have different newline styles.
I want to replace all newlines ‘\r\n’, ‘\n’,’\r’ with the same newline (in this case \r\n ).

What’s the fastest way to do this? My current solution looks like this which is way sucky:

    $sNicetext = str_replace("\r\n",'%%%%somthing%%%%', $sNicetext);
    $sNicetext = str_replace(array("\r","\n"),array("\r\n","\r\n"), $sNicetext);
    $sNicetext = str_replace('%%%%somthing%%%%',"\r\n", $sNicetext);

Problem is that you can’t do this with one replace because the \r\n will be duplicated to \r\n\r\n .

Thank you for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:33:10+00:00Added an answer on May 26, 2026 at 9:33 am
    $string = preg_replace('~\R~u', "\r\n", $string);
    

    If you don’t want to replace all Unicode newlines but only CRLF style ones, use:

    $string = preg_replace('~(*BSR_ANYCRLF)\R~', "\r\n", $string);
    

    \R matches these newlines, u is a modifier to treat the input string as UTF-8.


    From the PCRE docs:

    What \R matches

    By default, the sequence \R in a pattern matches any Unicode newline
    sequence, whatever has been selected as the line ending sequence. If
    you specify

         --enable-bsr-anycrlf
    

    the default is changed so that \R matches only CR, LF, or CRLF. Whatever is selected when PCRE is built can be overridden when the library
    functions are called.

    and

    Newline sequences

    Outside a character class, by default, the escape sequence \R matches
    any Unicode newline sequence. In non-UTF-8 mode \R is equivalent to the
    following:

        (?>\r\n|\n|\x0b|\f|\r|\x85)
    

    This is an example of an “atomic group”, details of which are given
    below. This particular group matches either the two-character sequence
    CR followed by LF, or one of the single characters LF (linefeed,
    U+000A), VT (vertical tab, U+000B), FF (formfeed, U+000C), CR (carriage
    return, U+000D), or NEL (next line, U+0085). The two-character sequence
    is treated as a single unit that cannot be split.

    In UTF-8 mode, two additional characters whose codepoints are greater
    than 255 are added: LS (line separator, U+2028) and PS (paragraph separator, U+2029). Unicode character property support is not needed for
    these characters to be recognized.

    It is possible to restrict \R to match only CR, LF, or CRLF (instead of
    the complete set of Unicode line endings) by setting the option
    PCRE_BSR_ANYCRLF either at compile time or when the pattern is matched.
    (BSR is an abbrevation for “backslash R”.) This can be made the default
    when PCRE is built; if this is the case, the other behaviour can be
    requested via the PCRE_BSR_UNICODE option. It is also possible to
    specify these settings by starting a pattern string with one of the
    following sequences:

        (*BSR_ANYCRLF)   CR, LF, or CRLF only
        (*BSR_UNICODE)   any Unicode newline sequence
    

    These override the default and the options given to pcre_compile() or
    pcre_compile2(), but they can be overridden by options given to
    pcre_exec() or pcre_dfa_exec(). Note that these special settings, which
    are not Perl-compatible, are recognized only at the very start of a
    pattern, and that they must be in upper case. If more than one of them
    is present, the last one is used. They can be combined with a change of
    newline convention; for example, a pattern can start with:

        (*ANY)(*BSR_ANYCRLF)
    

    They can also be combined with the (*UTF8) or (*UCP) special sequences.
    Inside a character class, \R is treated as an unrecognized escape
    sequence, and so matches the letter “R” by default, but causes an error
    if PCRE_EXTRA is set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file which I want to filter using awk. The text
I have a text file which needs to be constantly updated (regular intervals). All
(PHP 5.3.6) I have a php file which contains simply this text - there
I want to have a JTextField (or another text-like input which supports focus and
I have a text file which contains data seperated by '|'. I need to
I have a text box which has a Ajax calendar extender attached. You click
I currently have two text boxes which accept any number. I have a text
Hi I have a text field which I would like to bind to a
Lets say I have a text box which should have a default value.... and
I have a large text template which needs tokenized sections replaced by other text.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.