-1
$url = parse_url('https://plus.google.com/+erikedgren');
$address = ltrim(rtrim($url['path'], '/'), '/');
echo $address;

$address outputs plus.google.com/ erikedgren. Why? And how can I solve this problem?

EDIT

The code above outputs +erikedgren. But when I replace the address in $url with $_GET['u'] (from parse_url(https://plu...) to parse_url($_GET['u'])), I get plus.google.com/ erikedgren. Let me explain how my system works.

When I click on a link on my website on my local server (I haven't released the update yet), the URL for extern addresses looks like this: http://192.168.1.135/erik-edgren/url/https://plus.google.com/+erikedgren.

The last part with the extern address, is what $_GET['u'] is fetching. Here's how the RewriteRule looks like for it: RewriteRule ^url/(.*)$ get-url.php?u=$ 1 (no spaces between $ and 1).

Airikr
  • 6,258
  • 15
  • 59
  • 110
  • Copied your exact code, `$address` outputs "+erikedgren" as expected without the `plus.google.com` part. Unable to reproduce the problem. Please provide more information. – mister martin Apr 20 '16 at 18:58
  • @mistermartin That was really weird. Please see my updated question. – Airikr Apr 20 '16 at 19:08

2 Answers2

0

URL encode https://plus.google.com/+erikedgren before including it at the end of your URL. + is a space if it is not url encoded.

David Nguyen
  • 8,368
  • 2
  • 33
  • 49
  • Thank you but now I get the `Object not found` error. The URL is now `http://192.168.1.135/erik-edgren/url/https%3A%2F%2Fplus.google.com%2F%2Berikedgren`. If I click on another link that are not URL encoded, it works just fine. – Airikr Apr 20 '16 at 19:23
0

Based on your updated question, this appears to work for me:

$url = parse_url($_GET['u']);
$address = trim(rawurldecode(urlencode($url['path'])), '/');
echo $address;

I also replaced ltrim and rtrim, since trim will remove from both the beginning and end.

mister martin
  • 6,197
  • 4
  • 30
  • 63