作者:丰丰与鬼鬼_367 | 来源:互联网 | 2023-05-17 17:49
IhaveaMySQLtablewith2columns:我有一个包含2列的MySQL表:text:storesamulti-linetextwhichcoul
I have a MySQL table with 2 columns:
我有一个包含2列的MySQL表:
text
: stores a multi-line text which could have line breaks as \n, \r, \r\n...
. The text is inserted via a webform and could have been inserted by any browser on any OS.
text:存储多行文本,可以换行为\ n,\ r,\ r \ n ......文本通过webform插入,可以由任何操作系统上的任何浏览器插入。
line_break
: represents what type of line break this particular text will use (could be \n
,
,
, ...). This is also inserted via a webform by a user.
line_break:表示此特定文本将使用的换行符类型(可以是\ n,
,
,...)。这也是由用户通过webform插入的。
In PHP I then do the replacement:
在PHP中我然后做替换:
$text = preg_replace ("/(\r\n|\r|\n)/", $row['line_break'], $row['text']);
Interestingly, if the line break is stored in the DB as \n
I will actually show \n
instead of the newline. On the other hand, the following regexp will work correctly:
有趣的是,如果换行符存储在数据库中,我将实际显示\ n而不是换行符。另一方面,以下正则表达式将正常工作:
$text = preg_replace ("/(\r\n|\r|\n)/", "\n", $row['text']);
How do I need to store the newline in the DB for it to "work" correctly?
如何将换行符存储在数据库中以使其“正常”工作?
1 个解决方案