• 0

[php] Remove slash from beginning of REQUEST_URI generated link


Question

This is ruining my day and I'd appreciate some help.

 

I'm working on a simple portfolio website. The only php I use is to get the current URL in order to avoid using the regular social share buttons.

 

In case you don't know what I'm talking about, instead of adding the javascript buttons on the page, you can use simple links to share things on Facebook, Twitter etc. I'll use Google+ as an example, just for lolz.

 

To share a page on it, this is what you need:

<a href="https://plus.google.com/share?url=INSERT_URL">+1</a>

I used the REQUEST_URI function to get the current page url, in order to save time and not repeat the process manually for each individual page. The problem is I use datacontent-url to display the pages on top of index.html, and REQUEST_URI has no way of knowing that, so I had to tweak it a bit.

 

This would be the default code:

<a href="https://plus.google.com/share?url=<?php echo(('http://').$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]); ?>">+1</a>

This would output http://example.com/examplepage.html as the url for sharing, which I don't want. What I need is http://example.com/index.html#examplepage.html

 

So i tried this:

<a href="https://plus.google.com/share?url=<?php echo(('http://').$_SERVER["SERVER_NAME"].('/index.html#').$_SERVER["REQUEST_URI"]); ?>">+1</a>

The problem is that REQUEST_URI adds a slash at the beginning of its part, so I end up with a broken link:

http://example.com/index.html#/examplepage.html

Is there  way to remove the slash from the beginning of the url which is the result of the REQUEST_URI function?

 

Sorry if it's a dumb question, and I guess I can always update each individual portfolio page with custom, proper links, but I just figured this would save a lot of time. I tried examples from this page, but it doesn't work.

 

Thanks in advance.

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

You're trying to get url hash through php? If I'm correct that's impossible, instead just send the url on submit or whatever with js as a post variable.

Link to comment
Share on other sites

  • 0

You're trying to get url hash through php? If I'm correct that's impossible, instead just send the url on submit or whatever with js as a post variable.

The hash is included if you're loading the page, but not if you click a link on that page, it's just an indication of an anchor.

Link to comment
Share on other sites

  • 0
substr($_SERVER["REQUEST_URI"], 1);

 

 

Just use ltrim

ltrim($_SERVER['REQUEST_URI'], '/')

edit: too slow :p

 

Both of these do what I need. I have another question though REQUST_URI adds extra characters to my URL and I was wondering if you guys could figure out why it's happening.

 

This is the url I get:

http://example.com/index.html#examplepage.html?_=1396199176412

Why is it adding "?_=1396199176412" after .html because it breaks the url :/

Link to comment
Share on other sites

  • 0

Figured it out, ended up using PHP_SELF and it loses the extra parameters, giving me exactly what I need. In case anyone needs something similar:

<a href="https://plus.google.com/share?url=<?php echo(('http://').$_SERVER["SERVER_NAME"].('/index.html#').substr($_SERVER['PHP_SELF'], 1)); ?>">+1</a>

Again, huge thanks to both virtorio and James123. I'll mark it solved.

Link to comment
Share on other sites

This topic is now closed to further replies.