如何去除 Javascript (json) 中的斜杠?有 JQuery 插件吗?

发布于 2024-09-28 13:40:25 字数 531 浏览 4 评论 0原文

因此,当我将数据保存到数据库中时,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

地狱即天堂 2024-10-05 13:40:25

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

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.

寂寞花火° 2024-10-05 13:40:25

我会解决主要问题 - 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).

奢望 2024-10-05 13:40:25

也试试这个

function stripslashes (str) {

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

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;
    }
  });
}
旧情勿念 2024-10-05 13:40:25

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

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

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

}

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:

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文