javascript # 39;年代“unescape"在PHP中

Javascript's "unescape" in PHP

本文关键字:quot PHP 年代 javascript unescape      更新时间:2023-09-26

我有一个谷歌Chrome扩展为我的图像主机,它发送一个URL到我的网站。该URL通过Javascript的escape方法对进行编码。

escape编码的URL看起来像这样:

http%253A//4.bp.blogspot.com/-xa4Krfq2V6g/UF2K5XYv3kI/AAAAAAAAAJg/8wrqZQP9ru8/s1600/LuffyTimeSkip.png

我需要通过PHP以某种方式使URL恢复正常,因此我可以对filter_var($the_url, FILTER_VALIDATE_URL)进行检查,如果URL如上所述,它显然会失败。

这是Javascript的样子:

function upload(img) {
    var baseurl="http://imgit.org/remote?sent-urls=1&remote-urls=" + escape(img.srcUrl);
    uploadImage(baseurl);
}
function uploadImage(imgurl) {
        chrome.tabs.create({
        url: imgurl,
        selected: true
    });
}
var title = "Upload this image to IMGit";
var id = chrome.contextMenus.create({"title": title, "contexts": ['image'], "onclick": upload});

这是我在PHP中做的:

if (!filter_var($file, FILTER_VALIDATE_URL) || !filter_var(urldecode($file), FILTER_VALIDATE_URL))
{
    throw_error('The entered URLs do not have a valid URL format.', 'index.php#remote'); break;
}

坦率地说,urldecode()不适合我。正如你所注意到的,我通过$_GET接收URL。

处理这种情况的最好方法是什么?

实际问题:我如何unescape在PHP中转义的URL ?有更好的方法来处理这个问题吗?

您需要使用encodeURIComponent而不是escape:

function upload(img) {
    var baseurl="http://imgit.org/remote?sent-urls=1&remote-urls=" + encodeURIComponent(img.srcUrl);
    uploadImage(baseurl);
}

然后,您可以在PHP中使用urldecode来获得您想要的结果。

escape vs. encodeURI vs. encodeURIComponent的解释

For send in Javascript:

var baseurl="http://imgit.org/remote?sent-urls=1&remote-urls=" + encodeURIComponent(img.srcUrl);
PHP代码
$url = urldecode($_GET["my_url"])