Rman
Rman

Reputation: 19

How to replace Enters with '<br>' in a textarea php-posting?

I have a textarea , with some text and some enters. but I want to write all of the text into 1 line of a file in php. with <br> instead of enters. How can I do that?

Upvotes: 0

Views: 62

Answers (2)

bystwn22
bystwn22

Reputation: 1794

You can either use nl2br or str_replace function

Example:

Sample text

<?php
  $text  = 'line1
line2
line3
line4
line5';
?>

with nl2br

<?php
  echo nl2br( $text );
  /**
    line1<br />
    line2<br />
    line3<br />
    line4<br />
    line5
  **/
?>

with str_replace

<?php
  echo str_replace( PHP_EOL, "<br />", $text );
  /**
    line1<br />line2<br />line3<br />line4<br />line5
  **/
?>

Upvotes: 0

merlin2011
merlin2011

Reputation: 75649

I think you want to use nl2br if you are trying to replace all the newlines with <br>.

Upvotes: 2

Related Questions