如何使用PHP将带有换行符的文本字符串保存到MySQL数据库文本字段中? [英] How to save a text string with line breaks to a MySQL database text field using PHP?

查看:179
本文介绍了如何使用PHP将带有换行符的文本字符串保存到MySQL数据库文本字段中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SugarCRM中,我有一个Textarea字段,我试图通过换行符将数据保存到其中.

In SugarCRM I have a Textarea field that I am trying to save data to with line breaks.

如果我插入<br>标记,那么它们将显示为文本中断标记.如果我尝试使用\n之类的方法,它也会显示为文本.

IF I insert <br> tags, then they show up as text break tags. If I try something like \n then it also shows up as text.

现在,如果我进入SugarCRM并编辑记录,则可以输入文本并进行换行.保存记录并再次查看,然后出现换行符,就像我想要的一样!

Now if I go into SugarCRM and edit a record, I can type in text and make a linebreak. Save the record and view it again and the line breaks show up as I would want them to!

我正在尝试将带有换行符的数据从远程API脚本保存到同一字段.

I am trying to save data with line breaks to the same field from a remote API script.

我用phpMyAdmin进入数据库,以查看该字段的外观,这里是...

I went into the Database with phpMyAdmin to see how the field looked and here it is...

前3行具有正确的换行符,这是我在SugarCRM内手动编辑记录所产生的.

The first 3 lines have proper line breaks which I made inside SugarCRM editing a record manually.

下一行来自我的远程API脚本,在该脚本中我已将<br>标记保存到字符串中.

The line after are from my remote API script where I had saved <br> tags into the string.

Line 1 Text: ghfghdf
Line 1 Color: #0064FF
Line 1 Font: Architect
Line 2 Text: fghfg<br /> Line 2 Color: #9F00FF<br /> Line 2 Font: Belshaw<br /> Line 3 Text: fghdhdhdf<br /> Line 3 Color: #FF5000<br /> Line 3 Font: Benguiat<br />

有什么想法可以使我的PHP代码将字符串保存到此字段并在数据库中查看时像上面的前三行一样有换行符吗?

推荐答案

单引号字符串:

要指定文字单引号,请使用反斜杠(\)对其进行转义.要指定文字反斜杠,请将其加倍(\\).

To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash, double it (\\).

所有其他反斜杠实例将被视为文字反斜杠:这意味着您可能习惯使用的其他转义序列,例如\ r或\ n ,将按原样输出而不具有任何特殊含义.

All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.

但使用双引号字符串:

PHP将为特殊字符解释更多的转义序列

PHP will interpret more escape sequences for special characters

序列: \ n 然后具有换行的含义(LF或ASCII中的0x0A(10))

sequence : \n then having the meaning of linefeed (LF or 0x0A (10) in ASCII)

因此echo '\n'将打印 \ n echo "\n"允许您换行.

So echo '\n' will print \n while echo "\n" will allow you to have a new line as excepted.

当然,您还可以使用PHP_EOL使它跨操作系统.

And of course, you also can use PHP_EOL to make it cross OS.

这篇关于如何使用PHP将带有换行符的文本字符串保存到MySQL数据库文本字段中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆