如何去除 Javascript (json) 中的斜杠?有 JQuery 插件吗?
因此,当我将数据保存到数据库中时,PHP 会在单引号或双引号上添加 \。那很好。
但是,当使用json_encode()将数据传回客户端时;像 McDonald's 这样的文本在数据库中存储为 McDonald's,但是一旦从 PHP 传回 js,它将被编码为 McDonald's
因为我使用的是 jQuery,有没有任何插件可以轻松做到这一点?或者我应该使用什么函数来正确去除斜杠?显然,如果存在像\\\\s
这样的情况,函数应该返回\s
。 :)
对不起,大家。我想我把问题问得太复杂了。我让它更简单怎么样..
如果我有一个 javascript 变量:
var abc = "McDonald\'s";
var bcd = "I need a slash \\ ";
var cde = "save the double quote \"";
如何去掉 \' ?我应该使用什么正则表达式?
So, when I save the data into database, PHP will add a \ on single or double quotes. That is good.
However, when data is passed back to the client using json_encode(); TEXT like McDonald's is STORED as McDonald's in the DB but once passed back from PHP to js, it will be encoded as McDonald\'s
Since I'm using jQuery, is there any plugin to easily do that? or any function I should use to strip the slashes correctly? obviously, if there is case like \\\\s
, the function should return \s
. :)
Sorry guys. I think I made my question too complicated. How about I make it simpler..
If I have a javascript variable:
var abc = "McDonald\'s";
var bcd = "I need a slash \\ ";
var cde = "save the double quote \"";
how can I strip the \' ? what the regex I should use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
实际上,非常不鼓励使用这种插入斜杠的“魔术引号”功能。一般来说,您永远不想以转义格式将数据存储在数据库中;您想在输出中进行转义和编码。
It's actually highly discouraged to use this "magic quotes" feature that inserts slashes. In general, you never want to store data in the database in an escaped format; you want to do the escaping and encoding in the output.
我会解决主要问题 -
magic_quotes
已启用。我会禁用它并对数据库使用正确的转义方法。
那么您就不必担心 PHP 会神奇地添加斜杠。
如果您在使用 json_encode() 时谈论斜杠,那么它这样做是有原因的。
在 JavaScript 中使用 JSON 解析器,您将看不到它们(除非其他东西对它们进行了不正确的编码) )。
I would take care of the main problem -
magic_quotes
is enabled.I would disable it and use proper escaping methods with your database.
Then you don't have to worry about PHP magically adding slashes.
If you are talking about slashes when using
json_encode()
, it does that for a reason.Use a JSON parser in JavaScript and you won't see them (unless something else is improperly encoding them).
是的。
http://phpjs.org/functions/stripslashes:537
Yes.
http://phpjs.org/functions/stripslashes:537
也试试这个
Try this too
使用: http://au.php.net/manual /en/function.mysql-real-escape-string.php 在存储到数据库之前。
在写入任何用户界面之前使用这样的自定义函数:
Use: http://au.php.net/manual/en/function.mysql-real-escape-string.php before storing into database.
Use a custom function like this before writing onto any user interface: