如何使用PHP将带换行符的文本字符串保存到MySQL数据库文本字段中?

时间:2022-10-05 22:18:13

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

在SugarCRM中,我有一个Textarea字段,我试图用换行符保存数据。

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.

如果我插入
标记,那么它们将显示为文本中断标记。如果我尝试像\n这样的东西,它也会显示为文本。

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!

现在,如果我进入SugarCRM并编辑一个记录,我可以输入文本并进行换行。保存记录并再次查看它,行中断显示为我想要的!

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

我正在尝试从远程API脚本中将数据与行中断保存到相同的字段中。

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

我和phmypadmin一起进入数据库,看看这个字段是什么样子的。

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

前三行有适当的换行符,我在SugarCRM中手工编辑记录。

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

后面的行来自我的远程API脚本,我将
标记保存到字符串中。

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 />

Any ideas how I can make my PHP code save a string to this field and have line breaks when viewing it in the database just like the first 3 lines above are?

我如何能让我的PHP代码在这个字段中保存一个字符串,在数据库中查看它时,就像上面的前3行一样,有换行符?

3 个解决方案

#1


4  

As suggested in comments, this kind of issue tends to be related to the fact that in single quoted strings :

正如评论中提到的,这类问题往往与以下事实有关:

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

要指定一个文字单引号,用反斜杠(\)转义它。要指定文本反斜杠,请将其加倍(\)。

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.

所有其他的反斜杠实例都将被视为一个字面反斜杠:这意味着您可能使用的其他转义序列,如\r或\n,将按指定的方式输出,而不是具有任何特殊的含义。

but in double quoted strings :

但在双引号中:

PHP will interpret more escape sequences for special characters

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

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

序列:\n,表示换行(LF或0x0A (10))

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

所以echo '\n'将会打印\n,而echo "\n"将允许你有一个新的线路,除了。

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

当然,您也可以使用PHP_EOL使它与OS交叉。

#2


0  

When attempting to store data that has line breaks, you want to use the nl2br() function inside of php before you store the data. I had the same issues before and after months and months of trying different things to get it to work right on a client's site, it was on SO that I found the answer one day.

当尝试存储具有换行符的数据时,您希望在存储数据之前使用php内部的nl2br()函数。我在几个月前和几个月后都遇到了同样的问题,为了让它在客户的网站上正常运行,我尝试了几个月,直到有一天我找到了答案。

The key is to convert the data by using nl2br() before you store it.

关键是在存储数据之前使用nl2br()转换数据。

Hope this helps.

希望这个有帮助。

#3


0  

Use PHP_EOL instead of <br/> or \n

使用PHP_EOL代替
或\n

e.g.

如。

$array = array("a","b");
$string = implode(PHP_EOL , $array);

#1


4  

As suggested in comments, this kind of issue tends to be related to the fact that in single quoted strings :

正如评论中提到的,这类问题往往与以下事实有关:

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

要指定一个文字单引号,用反斜杠(\)转义它。要指定文本反斜杠,请将其加倍(\)。

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.

所有其他的反斜杠实例都将被视为一个字面反斜杠:这意味着您可能使用的其他转义序列,如\r或\n,将按指定的方式输出,而不是具有任何特殊的含义。

but in double quoted strings :

但在双引号中:

PHP will interpret more escape sequences for special characters

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

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

序列:\n,表示换行(LF或0x0A (10))

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

所以echo '\n'将会打印\n,而echo "\n"将允许你有一个新的线路,除了。

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

当然,您也可以使用PHP_EOL使它与OS交叉。

#2


0  

When attempting to store data that has line breaks, you want to use the nl2br() function inside of php before you store the data. I had the same issues before and after months and months of trying different things to get it to work right on a client's site, it was on SO that I found the answer one day.

当尝试存储具有换行符的数据时,您希望在存储数据之前使用php内部的nl2br()函数。我在几个月前和几个月后都遇到了同样的问题,为了让它在客户的网站上正常运行,我尝试了几个月,直到有一天我找到了答案。

The key is to convert the data by using nl2br() before you store it.

关键是在存储数据之前使用nl2br()转换数据。

Hope this helps.

希望这个有帮助。

#3


0  

Use PHP_EOL instead of <br/> or \n

使用PHP_EOL代替
或\n

e.g.

如。

$array = array("a","b");
$string = implode(PHP_EOL , $array);