如何在MySQL中存储转义字符并在php中显示它们? [英] How to store escaped characters in MySQL and display them in php?
问题描述
例如,我要存储字符串仅此而已". MySQL自动转义'字符.我如何使用php从数据库中回显该字符串,但在转义字符(如\')前删除\ ,?我还想保留其他格式,例如换行和空格.
您是否尝试过stripslashes()
,关于换行符只需使用nl2br()
函数即可.
示例:
$yourString = "That\'s all\n folks";
$yourString = stripslashes(nl2br($yourString));
echo $yourString;
注意:\\
双斜杠将变为\
单斜杠
您可能应该设置自己的功能,例如:
$yourString = "That\'s all\n folks";
function escapeString($string) {
return stripslashes(nl2br($string));
}
echo escapeString($yourString);
在nl2br()
文档 >
编辑2
您看到这些的原因是因为mysql转义了换行符,等等.我猜您正在使用mysql_*
函数.您可能应该查看mysqli
或PDO
.
这里是一个例子:
$yourString = "That's all
folks";
echo mysql_escape_string($yourString);
输出:
全部\ r \ n乡亲
For example I want to store the String "That's all". MySQL automatically escapes the ' character. How do I echo that String from the database using php but remove the \ in front of escaped characters like \' ? I would also like to preserve other formatting like new lines and blank spaces.
Have you tried stripslashes()
, regarding the linebreaks just use the nl2br()
function.
Example:
$yourString = "That\'s all\n folks";
$yourString = stripslashes(nl2br($yourString));
echo $yourString;
Note: \\
double slashes will turn to \
single slashes
You should probably setup your own function, something like:
$yourString = "That\'s all\n folks";
function escapeString($string) {
return stripslashes(nl2br($string));
}
echo escapeString($yourString);
There are also several good examples in the nl2br()
docs
Edit 2
The reason your are seeing these is because mysql is escaping line breaks, etc. I am guessing you are using mysql_*
functions. You should probably look into mysqli
or PDO
.
Here is an example:
$yourString = "That's all
folks";
echo mysql_escape_string($yourString);
Outputs:
That\'s all\r\n folks
这篇关于如何在MySQL中存储转义字符并在php中显示它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!