Convert BR to Newline

Technique #1

function br2newline( $input ) {
     $out = str_replace( "<br>", "\n", $input );
     $out = str_replace( "<br/>", "\n", $out );
     $out = str_replace( "<br />", "\n", $out );
     $out = str_replace( "<BR>", "\n", $out );
     $out = str_replace( "<BR/>", "\n", $out );
     $out = str_replace( "<BR />", "\n", $out );
     return $out;
}

Converts a break tag to a newline - no matter what kind of HTML is being processed.

Technique #2

function br2nl( $input ) {
 return preg_replace('/<br(\s+)?\/?>/i', "\n", $input);
}

Comments

  1. User Avatar
    Adam Haskell
    Permalink to comment#

    Technique #2 will work a lot better than #1
    But in #1, you could just use the first three calls and use str_ireplace() instead.

  2. User Avatar
    Vicky
    Permalink to comment#

    I’m suffered a lot. A used ” but I should use ‘. Could it be really a mistake?

  3. User Avatar
    Ronak Damani
    Permalink to comment#

    please help me out why is “i” used in
    function br2nl( $input ) {
    return preg_replace(‘//i’, “\n”, $input);
    }

    at the end of first parameter of preg_ replace

    • User Avatar
      Ronak Damani
      Permalink to comment#


      function br2nl( $input ) {
      return preg_replace('//i', "\n", $input);
      }

      sorry the code got messed up

    • User Avatar
      Matt
      Permalink to comment#

      to make the regex case insensitive

  4. User Avatar
    Vassi
    Permalink to comment#

    You can also do:


    function _br2nl($input)
    {
    return str_replace(array("", "", "", "", "", ""),"\n", $input );
    }

    • User Avatar
      Vassi
      Permalink to comment#

      <pre><code>
      function _br2nl($input)
      {
      return str_replace(array(“<br>”, “<BR>”, “<br/>”, “<BR/>”, “<br />”, “<BR />”),”\n”, $input );
      }
      </code></pre>

    • User Avatar
      Gastón Perez
      Permalink to comment#
      
      function _br2nl($input)
      {
      return str_replace(array('', '', ''),'\n', strtolower($input) );
      }
      
  5. User Avatar
    Alex
    Permalink to comment#

    `(\s+)?` could just be `\s*`.

  6. User Avatar
    gamehelp16
    Permalink to comment#

    It seems that you just need to use the nl2br() function

    thats all.

  7. User Avatar
    Dotan Cohen
    Permalink to comment#

    You can replace this:

    (\s+)?
    

    With this:

    \s*
    

    Here is how I would do it:

    preg_replace('/<br\s*\/?>/i', "\n", $input)
    

    But you should know that it won’t work on malicious input, such as:

    <br onmouseover="alert('pwnd')" />
    

    Here is a test of such input.

  8. User Avatar
    atinder pal singh

    is there any way to apply css style to br

  9. User Avatar
    biju
    Permalink to comment#

    this is help full. but i want to change in wordpress to . i tried remove_filter( ‘the_content’, ‘wpautop’ );
    remove_filter( ‘the_excerpt’, ‘wpautop’ ); but not work

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-closeicon-emailicon-linkicon-logo-staricon-menuicon-searchicon-staricon-tag