admin管理员组

文章数量:1399022

I have a Google Chrome extension for my image host and it send an URL to my website. That URL gets encoded via Javascript's escape method.

An URL encoded by escape looks like this:

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

I need to get the URL back to normal via PHP somehow, so I can check it against filter_var($the_url, FILTER_VALIDATE_URL) and it obviously fails if the URL is like above.

This is how the Javascript looks like:

function upload(img) {
    var baseurl=";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});

And this is what I do in 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;
}

Frankly, urldecode() doesn't do the job for me. And as you can notice, I am receiving the URL via $_GET.

What would be the best way to handle this situation?

Actual question: How do I unescape the escaped URL in PHP? Is there a better way to handle this problem?

I have a Google Chrome extension for my image host and it send an URL to my website. That URL gets encoded via Javascript's escape method.

An URL encoded by escape looks like this:

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

I need to get the URL back to normal via PHP somehow, so I can check it against filter_var($the_url, FILTER_VALIDATE_URL) and it obviously fails if the URL is like above.

This is how the Javascript looks like:

function upload(img) {
    var baseurl="http://imgit/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});

And this is what I do in 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;
}

Frankly, urldecode() doesn't do the job for me. And as you can notice, I am receiving the URL via $_GET.

What would be the best way to handle this situation?

Actual question: How do I unescape the escaped URL in PHP? Is there a better way to handle this problem?

Share Improve this question asked Jan 16, 2013 at 17:45 abortedaborted 4,55114 gold badges73 silver badges134 bronze badges 3
  • What's $the_url? Anything that es from $_GET is decoded automatically. – NullUserException Commented Jan 16, 2013 at 17:48
  • Could you post what the url looks like after urldecode has run it? Also you may want to look into encodeUri as an alternative to escape. This article may be of some help. Be advised that depending on the receiving server, it may not be able to properly decode the way escape encodes upper ASCII or non-ASCII characters. – War10ck Commented Jan 16, 2013 at 17:50
  • In your upload function, if you alert(img.srcUrl), do you get an already encoded url? – Lukas Commented Jan 16, 2013 at 18:04
Add a ment  | 

2 Answers 2

Reset to default 6

You'll want to use encodeURIComponent instead of escape:

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

Then, you can use urldecode inside PHP to get the result you want.

See this question for an explanation of escape vs. encodeURI vs. encodeURIComponent.

For send in Javascript:

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

In PHP code

$url = urldecode($_GET["my_url"])

本文标签: Javascript39s quotunescapequot in PHPStack Overflow