如何去除 Javascript (json) 中的斜线?任何 JQuery 插件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4011323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:08:04  来源:igfitidea点击:

How to strip slashes in Javascript (json)? Any JQuery plugin?

javascriptjquerystripslashes

提问by murvinlai

So, when I save the data into database, PHP will add a \ on single or double quotes. That is good.

因此,当我将数据保存到数据库中时,PHP 会在单引号或双引号上添加 \。那很好。

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

但是,当使用 json_encode(); 将数据传回客户端时;像麦当劳这样的文本在数据库中存储为麦当劳,但一旦从 PHP 传回给 js,它将被编码为麦当劳

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. :)

由于我使用的是 jQuery,是否有任何插件可以轻松做到这一点?或者我应该使用任何函数来正确去除斜线?显然,如果有类似\\\\s的情况 ,函数应该返回\s。:)

Sorry guys. I think I made my question too complicated. How about I make it simpler..

对不起大家。我想我的问题太复杂了。不如我简单点。。

If I have a javascript variable:

如果我有一个 javascript 变量:

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?

我怎样才能去掉 \' ?我应该使用什么正则表达式?

采纳答案by zerodin

Use: http://au.php.net/manual/en/function.mysql-real-escape-string.phpbefore storing into database.

在存储到数据库之前使用:http: //au.php.net/manual/en/function.mysql-real-escape-string.php

Use a custom function like this before writing onto any user interface:

在写入任何用户界面之前使用这样的自定义函数:

function unescape($string)
{

$search = array("\x00", "\n", "\r", "\\x1a");

$replace = array("\x00","\n", "\r", "\x1a");

$retString = str_replace($search, $replace, $string);

$search = array("\'", '\'.'"');

$replace = array(  "'", '"',);

$retString = str_replace($search, $replace, $retString);

$search = array("\\");

$replace = array( "\");

$retString = str_replace($search, $replace, $retString);

return $retString

}

回答by Domenic

It's actually highly discouraged to use this "magic quotes" featurethat 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.

实际上非常不鼓励使用这种插入斜杠的“魔术引号”功能。通常,您永远不想以转义格式将数据存储在数据库中;您想在输出中进行转义和编码。

回答by alex

I would take care of the main problem - magic_quotesis enabled.

我会解决主要问题 -magic_quotes已启用。

I would disable itand use proper escaping methods with your database.

我会禁用它并对您的数据库使用适当的转义方法。

Then you don't have to worry about PHP magicallyadding slashes.

那么你就不必担心 PHP神奇地添加斜线了。

If you are talking about slashes when using json_encode(), it does that for a reason.

如果您在使用 时谈论斜杠json_encode(),那么这样做是有原因的。

Use a JSON parserin JavaScript and you won't see them (unless something else is improperly encoding them).

在 JavaScript 中使用JSON 解析器,您将看不到它们(除非其他东西对它们进行了不正确的编码)。

回答by Jayanath

Try this too

也试试这个

function stripslashes (str) {

  return (str + '').replace(/\(.?)/g, function (s, n1) {
    switch (n1) {
    case '\':
      return '\';
    case '0':
      return '\u0000';
    case '':
      return '';
    default:
      return n1;
    }
  });
}