2

我的图像主机有一个 Google Chrome 扩展程序,它会向我的网站发送一个 URL。该 URL通过 Javascript 的方法进行编码。escape

由 编码的 URLescape如下所示:

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.

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

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

4

2 回答 2

6

您将要使用encodeURIComponent而不是escape

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

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

有关vs. vs.的解释,请参阅此问题escapeencodeURIencodeURIComponent

于 2013-01-16T17:54:55.487 回答
2

在 Javascript 中发送:

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

在 PHP 代码中

$url = urldecode($_GET["my_url"])
于 2013-01-16T18:31:42.673 回答