$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);
但是在数据库中仍然是新的一行。
在插入之前,我会htmlspecialchars(addslashes(trim($text)))
答案 0 :(得分:2)
为什么不使用nl2br()
功能?试试这个:
$text = nl2br($t);
而不是以下两行:
$order = array("\r\n", "\n", "\r");
$text = str_replace($order, '<br/>', $t);
答案 1 :(得分:2)
在DB中保存数据时,只需要进行转义以防止SQL注入。无需执行htmlspecialchars,nl2br,addslashes等。您可以按原样保存用户数据。但请确保其安全。 您应该在表示层显示此数据时使用htmlentities,nl2br,addslashes等功能。
答案 2 :(得分:0)
如果您根本不想要任何换行符,并且不希望将它们转换为HTML <br >
,请在此处查看:Remove all the line breaks from the html source
答案 3 :(得分:0)
您应该使用PHP内置方法nl2br()
nl2br — Inserts HTML line breaks before all newlines in a string
实施例
<?php
echo nl2br("Welcome\r\nThis is my HTML document", false);
?>
输出
Welcome<br>
This is my HTML document
答案 4 :(得分:0)
只需尝试$text = nl2br($t);