在html中显示mysql newline
-
19-09-2019 - |
题
我们的MySQL DB中的某些字段似乎包含newline字符,因此,如果我在它们上选择类似以下内容的内容,则将返回单个SQL调用:
Life to be sure is nothing much to lose
But young men think it is and we were young
如果我想在网页上显示此字段时保留线路断裂,是否是编写脚本以用BR HTML标签替换' n r'的标准解决方案,还是有更好的方法?
谢谢!
解决方案
在这里假设PHP ...
nl2br()
添加 <br />
每个 \n
. 。不要忘记首先逃脱内容,以防止XSS攻击。见下文:
<?php echo nl2br(htmlspecialchars($content)); ?>
不隶属于 StackOverflow