• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

[php] Remove slash from beginning of REQUEST_URI generated link

Question

Syanide    618

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.

Share this post


Link to post
Share on other sites

6 answers to this question

Recommended Posts

  • 0
+virtorio    3,128
substr($_SERVER["REQUEST_URI"], 1);
  • Like 2

Share this post


Link to post
Share on other sites
  • 0
James123    20

Just use ltrim

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

edit: too slow :p

  • Like 1

Share this post


Link to post
Share on other sites
  • 0
Seahorsepip    610

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.

Share this post


Link to post
Share on other sites
  • 0
n_K    2,431

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.

Share this post


Link to post
Share on other sites
  • 0
Syanide    618
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 :/

Share this post


Link to post
Share on other sites
  • 0
Syanide    618

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.