55

If I have a variable:

$var1 = "Line 1 info blah blah <br /> Line 2 info blah blah";

And a text area:

<textarea>echo $var1</textarea>

How can I get the text area to display a new line instead of displaying the text on a single like with a <br /> in it?

Edit: I have tried the following:

<textarea class="hobbieTalk" id="hobbieTalk" name="hobbieTalk" cols="35" rows="5" onchange="contentHandler('userInterests',this.id,this.value,0)"><?php

$convert=$_SESSION["hobbieTalk"];
$convert = str_replace("<br />", "\n", $convert);
echo $convert;

?></textarea>

However the text area still contains the br tags in the lines.

someguy
  • 553
  • 1
  • 4
  • 5
  • [I found this answer to be useful.](https://stackoverflow.com/questions/2494754/opposite-of-nl2br-is-it-str-replace) Didn't seem to match up with any of the answers above. – digifrog Jun 28 '17 at 15:20

6 Answers6

103

Try this one

<?php
    $text = "Hello <br /> Hello again <br> Hello again again <br/> Goodbye <BR>";
    $breaks = array("<br />","<br>","<br/>");  
    $text = str_ireplace($breaks, "\r\n", $text);  
?>  
<textarea><?php echo $text; ?></textarea>
Mobilpadde
  • 1,871
  • 3
  • 22
  • 29
  • 12
    thank you everyone: please note if you store this string in your db using converted html tags you must put: $breaks = array("
    ","
    ","
    ","
    ","<br />","<br/>","<br>"); cheers all
    – someguy May 16 '11 at 17:45
  • 3
    $text = str_ireplace($breaks, "", $text); worked better for me! :-) – bestprogrammerintheworld Jan 11 '16 at 07:59
  • @bestprogrammerintheworld for this $text = str_ireplace($breaks, "", $text); your can use `strip_tags($text)` – Naumov Feb 04 '16 at 10:51
  • @Naumov this will strip all tags from string, not only $breaks – Manuel Feb 10 '16 at 08:42
  • @ManuelDallaLana In question write what you need echo string in `` it means hi need only text no html entities. – Naumov Feb 10 '16 at 08:55
  • When writing text file, you can use PHP_EOL (it will add \r\n or \n depending on the platform(windows or linux) $text = str_ireplace($breaks, PHP_EOL, $text); – Ludo Oct 24 '18 at 14:59
  • It's a bad idea to replace HTML tags as if they are text constants. What if we get `< br>` or `
    `
    – Oleg Jun 06 '19 at 14:31
18

i am use following construction to convert back nl2br

function br2nl( $input ) {
    return preg_replace('/<br\s?\/?>/ius', "\n", str_replace("\n","",str_replace("\r","", htmlspecialchars_decode($input))));
}

here i replaced \n and \r symbols from $input because nl2br dosen't remove them and this causes wrong output with \n\n or \r<br>.

aftamat4ik
  • 718
  • 8
  • 14
5

The answer by @Mobilpadde is nice. But this is my solution with regex using preg_replace which might be faster according to my tests.

echo preg_replace('/<br\s?\/?>/i', "\r\n", "testing<br/><br /><BR><br>");

function function_one() {
    preg_replace('/<br\s?\/?>/i', "\r\n", "testing<br/><br /><BR><br>");
}

function function_two() {
    str_ireplace(['<br />','<br>','<br/>'], "\r\n", "testing<br/><br /><BR><br>");
}

function benchmark() {
    $count = 10000000;
    $before = microtime(true);

    for ($i=0 ; $i<$count; $i++) {
        function_one();
    }

    $after = microtime(true);
    echo ($after-$before)/$i . " sec/function one\n";



    $before = microtime(true);

    for ($i=0 ; $i<$count; $i++) {
        function_two();
    }

    $after = microtime(true);
    echo ($after-$before)/$i . " sec/function two\n";
}
benchmark();

Results:

1.1471637010574E-6 sec/function one (preg_replace)
1.6027762889862E-6 sec/function two (str_ireplace)
Jonathan
  • 10,936
  • 8
  • 64
  • 79
1

Here is another approach.

class orbisius_custom_string {
    /**
     * The reverse of nl2br. Handles <br/> <br/> <br />
     * usage: orbisius_custom_string::br2nl('Your buffer goes here ...');
     * @param str $buff
     * @return str
     * @author Slavi Marinov | http://orbisius.com
     */
    public static function br2nl($buff = '') {
        $buff = preg_replace('#<br[/\s]*>#si', "\n", $buff);
        $buff = trim($buff);

        return $buff;
    }
}
Svetoslav Marinov
  • 1,498
  • 14
  • 11
0

EDIT: previous answer was backwards of what you wanted. Use str_replace. replace <br> with \n

echo str_replace('<br>', "\n", $var1);
Crashspeeder
  • 4,291
  • 2
  • 16
  • 21
0
<?php

$var1 = "Line 1 info blah blah <br /> Line 2 info blah blah";
$var1 = str_replace("<br />", "\n", $var1);

?>

<textarea><?php echo $var1; ?></textarea>
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
Dan LaManna
  • 3,431
  • 4
  • 23
  • 35