php mysql n 转义字符串,如何在MySQL中存储转义字符并在php中显示它们?

"本文介绍如何在PHP中从MySQL数据库获取字符串"That's all",同时移除'转义字符,并保持格式如换行和空格。建议使用stripslashes()和nl2br()函数处理,或自定义函数进行字符串转换。提及了mysql_*函数的使用和mysqli或PDO的替代方案。"
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值