Reputation: 1914
$t = '"Confidence isn\'t gained over time and practice. Confidence is gained when you realize you choose your own path, you choose to fall, you choose not to fall.
If you are afraid to fall you fall because you are afraid. Everything is choice." - Daniel ILabaca';
$order = array("\r\n", "\n", "\r");
$text = str_replace($order, '<br/>', $t);
But in the database is still the new line.
Before inserting I do htmlspecialchars(addslashes(trim($text)))
Upvotes: 0
Views: 460
Reputation: 57650
While saving data in DB it only needs to be escaped to prevent SQL injection. No need to execute htmlspecialchars, nl2br, addslashes etc. You save the user data as it is. But make sure its safe. You should use htmlentities, nl2br, addslashes etc functions while displaying this data at the presentation layer.
Upvotes: 2
Reputation: 3064
Instead of the $order and str_replace bits, simply try $text = nl2br($t);
Upvotes: 0
Reputation: 8489
You should use PHP built-in method nl2br()
nl2br — Inserts HTML line breaks before all newlines in a string
Example
<?php
echo nl2br("Welcome\r\nThis is my HTML document", false);
?>
Output
Welcome<br>
This is my HTML document
Upvotes: 0
Reputation: 2194
If you don't want any linebreaks at all and don't want to have them converted to HTML <br >
have a look at this here: Remove all the line breaks from the html source
Upvotes: 0
Reputation: 6071
Why don't you have a go with the nl2br()
function? Try this:
$text = nl2br($t);
Instead of the following two rows that is:
$order = array("\r\n", "\n", "\r");
$text = str_replace($order, '<br/>', $t);
Upvotes: 2