javascript jQuery 和 PHP:编码和解码 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7296499/
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-10-25 23:38:21  来源:igfitidea点击:

jQuery & PHP: Encode & Decode URL

phpjavascriptjqueryuriurlencode

提问by stefmikhail

I am passing a string through a URL to another page whenever a link is clicked.

每当单击链接时,我都会通过 URL 将字符串传递到另一个页面。

The original atag and its hrefare created via a PHP while loop as follows:

原始a标签及其href通过 PHP while 循环创建,如下所示:

<a href=".($row['secondary'] == 'true' ? "secondary_imgs.php?imgId=".$row['imgId']."
    &imgTitle=".$row['imgTitle']."" : "new_arrivals_img/".$row['imgURL']."").">

Note:The atag doescontain other attributes but I removed them for simplicity.

注意:a标签确实包含其他属性,但为了简单起见,我删除了它们。

The hrefis then appended, with the current window dimensions, using the following jQuery function:

href然后被附加,在当前窗口的尺寸,使用下列jQuery函数:

$("a[href^='secondary_imgs.php']").click(function(){
    var pageWidth = $(window).width();
    var pageHeight = $(window).height();
    var url = $(this).attr('href');
    var idx = url.indexOf('&pageWidth=');
    if (idx != -1) {
        url = url.substr(0, idx);
    }
    url += '&pageWidth=' + pageWidth + '&pageHeight=' + pageHeight;
    $(this).attr('href', url);
});

My question is, what method(s) of encoding the URL should I use in this case, seeing as it will need to be done with first PHP, then jQuery/JavaScript, and finally decoded via PHP? Or willit need to be done in both PHP and jQuery/JavaScript? The original PHP $row['imgTitle']will alwayshave at least spaces in it, and so I know it needs to be done then for sure. But seeing as the appended values will always be a variable equalling a number, does that need to be encoded as well?

我的问题是,在这种情况下我应该使用哪种编码 URL 的方法,因为它需要首先使用 PHP,然后是 jQuery/JavaScript,最后通过 PHP 解码?或者它必须同时在PHP和jQuery / JavaScript来做些什么呢?原来PHP$row['imgTitle']始终至少有空间在里面,所以我知道它需要,于是做了肯定。但是看到附加值总是一个等于数字的变量,是否也需要编码?

I know there are several methods of encoding/decoding URLs in each coding language, and my knowledge isn't adequate enough to make the choice.

我知道每种编码语言都有多种编码/解码 URL 的方法,但我的知识不足以做出选择。

If someone could also provide an example of how I would perform each step, it would be much appreciated.

如果有人还可以提供我将如何执行每个步骤的示例,将不胜感激。

回答by Arnaud Le Blanc

You should use urlencode()in PHP to encode URI components.

您应该urlencode()在 PHP 中使用来编码 URI 组件。

The equivalent function in javascript is encodeURIComponent().

javascript 中的等效函数是encodeURIComponent().

PHP will decodeparameters encoded like this automatically.

PHP 会自动解码这样编码的参数。

So, in you PHP:

所以,在你的 PHP 中:

'<a href="..&imgTitle='.urlencode($row['imgTitle'])

And equivalently, in your javascript:

同样,在您的 javascript 中:

'&someparam=' + encodeURIComponent(someValue)

回答by AlienWebguy

Why would it need to be done first with PHP? This JS urlencode function is able to be urldecoded from PHP. Encode once, decode once:

为什么需要先用 PHP 来完成?这个 JS urlencode 函数可以从 PHP 中进行 urldecode。编码一次,解码一次:

function urlencode (str) {
    // URL-encodes string  
    // 
    // version: 1107.2516
    // discuss at: http://phpjs.org/functions/urlencode
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Joris
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // %          note 1: This reflects PHP 5.3/6.0+ behavior
    // %        note 2: Please be aware that this function expects to encode into UTF-8 encoded strings, as found on
    // %        note 2: pages served as UTF-8
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
    str = (str + '').toString();

    // Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
    // PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
    replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}