php 使用 JavaScript 去除斜线?可能的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5326165/
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
Use JavaScript to stripslashes ? possible
提问by RIK
I'm using ajax to grab a URL. The problem is the URL has slashes in it and when the JQuery load takes place afterwords it will not load the page.
我正在使用 ajax 来获取 URL。问题是 URL 中有斜杠,当 JQuery 加载发生后,它不会加载页面。
AJAX success bit:
AJAX 成功位:
success: function(data) {
$('#OPTcontentpanel').load(data.OPTpermalink);
PHP
PHP
echo json_encode( array('OPTpermalink'=>$OPTpermalink,));
AND the response
和响应
http:\/\/www.divethegap.com\/update\/options\/padi-open-water\/
So need to strip the slashes. I know how to do it in PHP but not in AJAXJavaScript.
所以需要去掉斜线。我知道如何在 PHP 中做到这一点,但在AJAXJavaScript。
Any ideas?
有任何想法吗?
Marvellous
奇妙
回答by ColBeseder
A new answer to an old question:
一个老问题的新答案:
String.prototype.stripSlashes = function(){
return this.replace(/\(.)/mg, "");
}
Example of use:
使用示例:
var str = "You\'re slashed \/\..\/\"; // Text from server
str = str.stripSlashes() ;
output:
输出:
You're slashed /\../\
回答by codeguerrilla
This is an old post but thought I would add my answer, seems more efficient than some other answers here:
这是一篇旧帖子,但我想我会添加我的答案,似乎比这里的其他一些答案更有效:
var url = "http:\/\/www.divethegap.com\/update\/options\/padi-open-water\/"
var res = url.replace(new RegExp("\\", "g"), "");
This will replace all occurrences of a backslash character with nothing.
这将用空替换所有出现的反斜杠字符。
回答by Billy Moon
There has been a good port of many of php's core functions, including stripslashes
over here: http://phpjs.org/functions/stripslashes/
许多 php 的核心功能都有一个很好的移植,包括stripslashes
在这里:http: //phpjs.org/functions/stripslashes/
function stripslashes (str) {
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Ates Goral (http://magnetiq.com)
// + fixed by: Mick@el
// + improved by: marrtins
// + bugfixed by: Onno Marsman
// + improved by: rezna
// + input by: Rick Waldron
// + reimplemented by: Brett Zamir (http://brett-zamir.me)
// + input by: Brant Messenger (http://www.brantmessenger.com/)
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// * example 1: stripslashes('Kevin\'s code');
// * returns 1: "Kevin's code"
// * example 2: stripslashes('Kevin\\'s code');
// * returns 2: "Kevin\'s code"
return (str + '').replace(/\(.?)/g, function (s, n1) {
switch (n1) {
case '\':
return '\';
case '0':
return '\u0000';
case '':
return '';
default:
return n1;
}
});
}
回答by Pekka
You're sending JSON, but inserting it directly into a HTML element. That is not wise, can create broken results, and probably not what you want to do in the first place.
您正在发送 JSON,但将其直接插入到 HTML 元素中。那是不明智的,会产生破坏性的结果,而且可能不是您首先想要做的。
You should probably either
你应该要么
change the PHP script's output to create proper HTML
expect JSON on the JavaScript side (using jQuery's
dataType
parameter, or the shorthand$.json()
, and parse that
更改 PHP 脚本的输出以创建正确的 HTML
期望 JavaScript 端的 JSON(使用 jQuery 的
dataType
参数,或简写$.json()
,并解析它
回答by SadullahCeran
Have you tried string.replace?
你试过 string.replace 吗?
success: function(data) {
$('#OPTcontentpanel').load(data.OPTpermalink.replace("\", ""));