0

I wrote a php site (it's still a prototype) and I placed a Digg button on it. It was easy but...

The official manual says: "the URL has to be encoded". I did that with urlencode(). After urlencode, my URL looks like this:

http%3A%2F%2Fwww.mysite.com%2Fen%2Fredirect.php%3Fl%3Dhttp%3A%2F%2Fwww.othersite.rs%2FNews%2FWorld%2F227040%2FRusia-Airplane-crashed%26N%3DRusia%3A+Airplane+crashed

So far it's good, but when I want to submit that URL to Digg, it is recognized as an invalid URL:

http://www.mysite.com/en/redirect.php?l=http://www.othersite.rs/News/World/227040/Rusia-Airplane-crashed&N=Rusia:+Airplane crashed

If I place a "+" between "Airplane" and "crashed" (at the end of the link), then Digg recognizes it without any problems!

Please help, this bizarre problem is killing my brain cells!

P.S. For purpose of this answer, urls are changed (to nonexisting ones) because, in the original, non-english sites are involved.

Esoteric Screen Name
  • 6,082
  • 4
  • 29
  • 38
guest86
  • 2,894
  • 8
  • 49
  • 72

1 Answers1

0

After you've urlencode()ed it, encode the resulting plus signs as well:

$encoded_url = urlencode($original_url);
$final_url = str_replace('+', '%2B', $encoded_url);

Or alternatively, you could replace spaces in your URL with + first, and then urlencode() the result:

$spaceless_url = str_replace(' ', '+', $original_url);
$final_url = urlencode($spaceless_url);

If your own site required the parameters in the query string to be encoded in the first place, you wouldn't have the issue (since there wouldn't be an unencoded space in the original URL).

Amber
  • 507,862
  • 82
  • 626
  • 550
  • It won't do the trick! Interesting is that, if I "die($link)" it looks fine, (+ instead of " "). But, on digg page it is interpreted with only + instead of first " "! Take a look at "&N=Rusia:+Airplane crashed" part. If i omit "N" parameter in url, it's working fine, but i HAVE TO have that "N"! – guest86 Jan 01 '11 at 15:00
  • It needs to be `%2B` - not `+`, and not a space. – Amber Jan 01 '11 at 15:34
  • OMG! I got it! If i remove non-english characters from "N" argument it's ok! Is there any way to properly "urlencode" text with non-english latin characters? – guest86 Jan 01 '11 at 16:13