WordPress.org

Make WordPress Core

Opened 2 months ago

Last modified 2 months ago

#51040 new feature request

How about having a native BR to Newline method, i.e. br2nl

Reported by: subrataemfluence Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Formatting Keywords: 2nd-opinion
Focuses: Cc:

Description

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
/**
 * @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 ) ) ) );
}

Change History (3)

#2 in reply to: ↑ 1 @subrataemfluence
2 months ago

What's happening here?! I request the concerned team take necessary action ikmmediately, please.

Replying to techunz:
~link removed~

Last edited 2 months ago by johnbillion (previous) (diff)

#3 follow-up: @johnbillion
2 months ago

  • Component changed from General to Formatting
  • Type changed from enhancement to feature request
  • Version 5.5 deleted

@subrataemfluence This was a spam comment.

#4 in reply to: ↑ 3 @subrataemfluence
2 months ago

Thank you @johnbillion for taking immediate action and removing the link :)

Replying to johnbillion:

@subrataemfluence This was a spam comment.

Note: See TracTickets for help on using tickets.