Php echo textarea line break

I'm using an textarea to send text to my DB.

Screenshot of db:

Php echo textarea line break

When I'm reading from the DB it removes the line breaks, how can I keep them in $row['opmerkingen']?

Php echo textarea line break

asked Jun 14, 2011 at 14:00

Php echo textarea line break

1

When displaying text, use nl2br() to convert newlines to
tags, i.e., instead of , use .

By default, browsers display newlines as spaces, therefore they have to be converted to
tags.


For those who find this useful - please consider using white-space: pre-line, suggested by Emil Vikström. I'm not a web guy anymore and easily can't verify this, but Boaz says in comments that it is supported by all modern browsers. If so, that should be preferred to using nl2br().

answered Jun 14, 2011 at 14:01

binaryLVbinaryLV

8,8322 gold badges39 silver badges42 bronze badges

1

An alternative to nl2br is to make use of the CSS attribute white-space:

white-space: pre-line;

answered Jun 14, 2011 at 14:17

Emil VikströmEmil Vikström

88.4k15 gold badges134 silver badges170 bronze badges

1

I put as follows but not working with single quotes.

echo $row['text'].'\n';

Put the double quotes. Then worked.


When we getting data it is comming with \r\n. Also use the double quotes there.

answered May 21, 2013 at 13:02

Sumith HarshanSumith Harshan

6,0782 gold badges35 silver badges35 bronze badges

0

Not the answer you're looking for? Browse other questions tagged php html textarea or ask your own question.

    Table of contents
  • Add Line Break in PHP
  • How to add a line break within echo in PHP? [closed]
  • How to echo an input of an textarea with line breaks?
  • How To echo a line break in the browser?
  • How do I echo line breaks from a MySQL database?
  • How to add a new line within echo in PHP?
  • Php mysql storing line breaks in text area in database
  • PHP - How To Add Line Break in PHP

Add Line Break in PHP

# php 7.*

sun 
moon
#php 7.x
 
Sun 
 Moon
#php 7.x
". "Have a great time here"
?>
Welcome to Delft
Have a great time here

How to add a line break within echo in PHP? [closed]

echo "Thanks for your email. /n  Your orders details are below:".PHP_EOL;
echo 'Thanks for your email. /n  Your orders details are below:'.PHP_EOL;
echo  "kings \n garden";
kings garden
echo  nl2br ("kings \n garden");
kings
garden
so "\n" not '\n'
$myfile = fopen("test.txt", "w+")  ;

$txt = "kings \n garden";
fwrite($myfile, $txt);
fclose($myfile);
 
kings
 garden
echo "Thanks for your email" ."
". "Your orders details are below:"
echo __("Thanks for your email.\n
\n
Your order's details are below:", 'jigoshop');

Add Line Break in PHP

# php 7.*

sun 
moon
#php 7.x
 
Sun 
 Moon
#php 7.x
". "Have a great time here"
?>
Welcome to Delft
Have a great time here

How to echo an input of an textarea with line breaks?

white-space: pre-line;
echo $row['text'].'\n';

How To echo a line break in the browser?

if (file_exists($fName)) {
   echo "CreationTime: ".$CreationTime.
        "CurrentTime: ".$CurrentTime.
        "after ".($fLifeTime)." Days from Creation: ".$fAge;
   }
if (file_exists($fName)) {
   echo "CreationTime: ".$CreationTime. "
". "CurrentTime: ".$CurrentTime. "
" . "after ".($fLifeTime)." Days from Creation: ".$fAge; }
 if (file_exists($fName)) {
      echo "
";
       echo "CreationTime: ".$CreationTime. PHP_EOL .
            "CurrentTime: ".$CurrentTime. PHP_EOL .
            "after ".($fLifeTime)." Days from Creation: ".$fAge;
       }
if (file_exists($fName)) {
    echo "CreationTime: ".$CreationTime.
    "
CurrentTime: ".$CurrentTime. "
after ".($fLifeTime)." Days from Creation: ".$fAge; }
if (file_exists($fName)) {
   echo "CreationTime: ".$CreationTime.
        "CurrentTime: ".$CurrentTime.
        "after ".($fLifeTime)." Days from Creation: ".$fAge;
echo "
"; //break to new line. }
if (!file_exists($fName)) {
    $info = "CreationTime: " . $CreationTime . PHP_EOL .
        "CurrentTime: ". $CurrentTime . PHP_EOL .
        "after ". ($fLifeTime)." Days from Creation: " . $fAge;

    echo nl2br($info);
}

How do I echo line breaks from a MySQL database?

while($wallposts = mysql_fetch_assoc($getwallposts)) {
    $postid = $wallposts['id'];
    $postedby_username = $wallposts['postedby'];
    $wallpostdate = $wallposts['dateposted'];
    $wallpost = $wallposts['post'];

    $querypostedby_info = mysql_query("SELECT * FROM `users` WHERE `username`='$postedby_username'");

    //get the info above
    if (mysql_num_rows($querypostedby_info)===1) {
        $getpostedby_info = mysql_fetch_assoc($querypostedby_info);

        $postedby_id = $getpostedby_info['id'];
        $postedby_profilepicture = $getpostedby_info['profilepicture'];
    }

    //display the posts
    $wallpoststicker = 
    "
    
• $wallpostdate
$wallpost
"; }

How to add a new line within echo in PHP?

ABC
This is a line break: 
This line break is ignored: \n(It's not a new line.)
string(5) "A
B
C"
That creates a new line:
New line!
\n\n
");
    echo("(3) The following creates a line break in both cases:
\n"); ?>
(1) This line break is only displayed in the source code of a browser: 
(2) This line break is displayed by the browser, but does not create a line break in the source text: 
(3) The following creates a line break in both cases:
(1) This line break is only displayed in the source code of a browser: (2) This line break is displayed by the browser, but does not create a line break in the source text: 
(3) The following creates a line break in both cases:

Php mysql storing line breaks in text area in database

Some text some text some text some text


new line some text some text new line 

new line some text new line some text
$sql = "insert into people (price, contact, category, username, fname, lname, expire, filename) values (:price, :contact, :category, :user_id, :fname, :lname, now() + INTERVAL 1 MONTH, :imagename)";
$q = $conn->prepare($sql) or die("failed!");
$q->bindParam(':price', $price, PDO::PARAM_STR);
$q->bindParam(':contact', $contact, PDO::PARAM_STR);
$q->bindParam(':category', $category, PDO::PARAM_STR);
$q->bindParam(':user_id', $user_id, PDO::PARAM_STR);
$q->bindParam(':fname', $fname, PDO::PARAM_STR);
$q->bindParam(':lname', $lname, PDO::PARAM_STR);
$q->bindParam(':imagename', $imagename, PDO::PARAM_STR);
$q->execute();
str.replace("\n", "
","g");
$query = "UPDATE your_table 
         SET your_text_field = 'line 1'" . '\n' . "'line2' 
         WHERE your_id_field = " . $id . "';";

PHP - How To Add Line Break in PHP

# php 7.*  
sun  moon 
#php 7.x   
Sun   Moon 
#php 7.x ". "Have a great time here" ?> 
Welcome to Delft Have a great time here 

Next Lesson PHP Tutorial

How to insert a line break in PHP?

The nl2br() function inserts HTML line breaks (
or
) in front of each newline (\n) in a string.

How to skip a line in PHP?

Answer: Use the Newline Characters ' \n ' or ' \r\n ' You can use the PHP newline characters \n or \r\n to create a new line inside the source code. However, if you want the line breaks to be visible in the browser too, you can use the PHP nl2br() function which inserts HTML line breaks before all newlines in a string.

How do I preserve line breaks in textarea?

4 Answers.
white-space: pre-line; whitespace trimmed to single whitespace or..
white-space: pre-wrap; all whitespace preserved..

How do you store line breaks in database?

Just put the output text between
 tags
, that will preserve the line breaks.