javascript 从 URL 参数中删除 %20
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15102209/
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
Removing %20 from URL parameters
提问by Matthew Paxman
I know you need to use some variant of decodeURIComponent() to do something like this, but since I'm still fairly new to coding and using some code I found on the net for my purposes, I'm not sure how to go about changing it to suit my needs.
我知道你需要使用 decodeURIComponent() 的一些变体来做这样的事情,但由于我对编码和使用我在网上找到的一些代码仍然很陌生,我不知道如何去做改变它以适应我的需要。
What I have is a function that gets each URL parameter I need out of the URL (of which there are many). I have to use these variables for other functions as parameters and also to display on the page, and I can't get the %20's to disappear.
我拥有的是一个函数,可以从 URL(其中有很多)中获取我需要的每个 URL 参数。我必须将这些变量用作其他函数的参数并显示在页面上,而且我无法让 %20 消失。
function getUrlVars() {
var vars = {};
parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
Where I get each variable using:
我在哪里使用以下方法获取每个变量:
var markname = getUrlVars()["mname"];
I've tried to put decodeURIComponent() in different places in that function, but I can't seem to get it to work. I'm also not sure if it needs to use value or vars.
我试图将 decodeURIComponent() 放在该函数的不同位置,但我似乎无法让它工作。我也不确定它是否需要使用 value 或 vars。
value = decodeURIComponent(value);
Or something like that...
或类似的东西...
Any help would be appreciated! Thanks!
任何帮助,将不胜感激!谢谢!
回答by Bergi
decodeURIComponent
as you posted should work fine. You might as well replace plus signs with spaces, and don't forget to decode the key
as well:
decodeURIComponent
正如您发布的那样应该可以正常工作。您也可以用空格替换加号,并且不要忘记解码key
:
function getUrlVars() {
var url = window.location.href,
vars = {};
url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
key = decodeURIComponent(key);
value = decodeURIComponent(value);
vars[key] = value;
});
return vars;
}
回答by Rahul Tathod
when you pass the url use
str_replace(" ","-",$name)
and decode it by
str_replace("-"," ",$p->property_name)
当您通过 url 使用str_replace(" ","-",$name)
并通过以下方式
对其进行解码时
str_replace("-"," ",$p->property_name)
it will remove space and add - in the url
它将删除空间并在 url 中添加 -