Reputation: 12856
I am creating an application using PHP and MySQL.
In the DB, the data is saved in the following manner:
But, while displaying the result in HTML page, the result comes like this:
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>
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
Reputation: 764
Use the below simple CSS
.com-text{
white-space: pre-wrap;
word-wrap: break-word;
}
Upvotes: 6
Reputation: 1720
use nl2br();
<div class="com-text">
<?php echo nl2br($row['chatDesc']) ;?>
</div>
Upvotes: 1
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
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
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
Reputation: 367
$orig = "I'll \"walk\" the dog now";
$a = htmlentities($orig);
$b = html_entity_decode($a);
Upvotes: 1
Reputation: 1544
You can try this.
<div class="com-text">
<pre> <?php echo $row['chatDesc'];?> </pre>
</div>
Upvotes: 1