Saswat
Saswat

Reputation: 12856

How to display text saved in DB, properly in HTML?

I am creating an application using PHP and MySQL.

In the DB, the data is saved in the following manner:

DB-Data

But, while displaying the result in HTML page, the result comes like this:

HTML display

The issue might be because HTML needs tags like "<br/>", etc......

How can I display the result in actual format?

Here's the code section displaying the data:

<div class="com-text">
    <?php echo $row['chatDesc'];?>
</div>

EDIT:

Also, I need to display the data by creating a div using jquery(dynamically). So how to display it in javascript format too?

html += '<div class="com-text">'+data.chat[i].chatDesc+'</div>';
$("#chat_list").append(html);

Upvotes: 2

Views: 4479

Answers (7)

Kiran Reddy
Kiran Reddy

Reputation: 764

Use the below simple CSS

.com-text{
    white-space: pre-wrap;
      word-wrap: break-word;
}

Upvotes: 6

Shadi Shaaban
Shadi Shaaban

Reputation: 1720

use nl2br();

<div class="com-text">
    <?php echo nl2br($row['chatDesc']) ;?>
</div>

Upvotes: 1

Seyed Morteza Hosseini
Seyed Morteza Hosseini

Reputation: 402

You should use nl2br which inserts line breaks where newlines occur in a string

the javascript equivement of this function + your code:

function nl2br (str, is_xhtml) {   
  var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : 

  '<br>';    
  return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}

html += '<div class="com-text">'+nl2br(data.chat[i].chatDesc, true)+'</div>';
$("#chat_list").append(html);

Upvotes: 1

Asfandyar Khan
Asfandyar Khan

Reputation: 1738

Just use the nl2br() function to format it:

<?php

$mychat = nl2br($row['chatDesc']);

?>

Here is how you can do..

 <div class="com-text">
 <?php echo $mychat; ?>
 </div>

Upvotes: 1

The Codesee
The Codesee

Reputation: 3783

You should use nl2br which inserts line breaks where newlines occur in a string

<div class="com-text">
    <?php echo nl2br($row['chatDesc']);?>
</div>

Upvotes: 1

suman das
suman das

Reputation: 367

$orig = "I'll \"walk\" the dog now";

$a = htmlentities($orig);

$b = html_entity_decode($a);

Upvotes: 1

SAUMYA
SAUMYA

Reputation: 1544

You can try this.

<div class="com-text">
   <pre> <?php echo $row['chatDesc'];?> </pre>
</div>

Upvotes: 1

Related Questions