[wp-trac] [WordPress Trac] #51040: How about having a native BR to Newline method, i.e. br2nl

WordPress Trac noreply at wordpress.org
Mon Aug 17 09:54:59 UTC 2020


#51040: How about having a native BR to Newline method, i.e. br2nl
------------------------------+-----------------------------
 Reporter:  subrataemfluence  |      Owner:  (none)
     Type:  enhancement       |     Status:  new
 Priority:  normal            |  Milestone:  Awaiting Review
Component:  General           |    Version:  5.5
 Severity:  normal            |   Keywords:  2nd-opinion
  Focuses:                    |
------------------------------+-----------------------------
 Is there a specific reason that WordPress does not have yet (?) a function
 to convert Line break (`<Br />`) to Newline (`\n`)?

 This is useful for `textarea` fields.

 One of the use cases could be a multiline address used as metadata in the
 user profile.

 In the `textarea` user enters like:


 {{{
 Building name <Return>
 Street address <Return>
 Postal code. Country name
 }}}


 When saved we usually convert all line breaks into HTML line breaks for
 easier rendering in the front-end. We do this using `nl2br` method. So in
 the database, the above becomes:


 {{{
 Building name <br />
 Street address <br />
 Postal code. Country name

 }}}

 However, when the above information is populated in edit mode, it comes up
 like


 {{{
 Building name <br />
 Street address <br />
 Postal code. Country name.
 }}}


 A `br2nl` method will make life much easier.

 {{{#!php
 <?php
 /**
  * @param string $input_string
  * Converts HTML line break (<br />) into newline character (\n)
  *
  * @return null|string|string[]
  * Since 5.5
  */
 function br2nl( $input_string ) {
    return preg_replace('/<br\s?\/?>/ius', "\n", str_replace( "\n", "",
 str_replace( "\r", "", htmlspecialchars_decode( $input_string ) ) ) );
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/51040>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list