• 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.

6 answers to this question

Recommended Posts

  • 0
  On 30/03/2014 at 13:28, Seahorsepip said:

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.

  • 0
  On 30/03/2014 at 12:38, virtorio said:
substr($_SERVER["REQUEST_URI"], 1);

 

 

  On 30/03/2014 at 12:38, James123 said:

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 :/

  • 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.

This topic is now closed to further replies.
  • Posts

    • Toshiba's massive 22TB X300 PRO hard drive is 29% off by Taras Buria Toshiba is offering its highest-capacity X300 PRO hard drive at a new all-time low price, allowing you to save 29% or $171 on the massive 22TB drive. The X300 PRO hard drive lineup offers good performance, and it is rated for high yearly workloads, which makes it a great option for creative professionals or other tasks that require storing a lot of data. The X300 PRO is a 7,200 RPM conventional magnetic recording (CMR) 3.5-inch SATA 6Gbps internal hard drive with 512MB of cache, which is also paired with the Toshiba Cache Technology for improved real-time responsiveness during high-demand loads. It also has additional features for better reliability, such as ramp loading (heads rest to a side of platters to prevent damage when the drive is idling or powered down) and shock sensors. Toshiba claims that the 22TB X300 PRO is rated for up to a 300TB/year workload rate and 1 million hours of mean time to failure (MTTF). This makes the drive a good choice for workstations, multimedia systems, or network-attached storage. Additionally, each drive has a limited five-year warranty for additional peace of mind. 22TB Toshiba X300 PRO High Workload Performance HDD - $428.99 | 29% off on Amazon US This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • The 4060 is the weak 8-GB version. They should have done better.
    • This is an example of why it is so difficult to have a conversation with conservatives - they refuse to operate in good faith. You say "Those are not rights. Those were special treatments that were taken away that non-trans whatever didn't get." Which means you either failed to read any of the links I provided or you are lying. The very first link is about how the U.S. Military is firing trans people out of the military because they are trans and denying them retirement benefits. What other groups does the military treat this way that would support your assertion that they had been treated as special previously? Does the miliary routinely fire large numbers of its members and deny them retirement and was heretofore not doing that to trans people? I fail to see the logic in your argument.
  • Recent Achievements

    • One Month Later
      Jaclidio hoy earned a badge
      One Month Later
    • Week One Done
      Yawdee earned a badge
      Week One Done
    • Week One Done
      eugwalker earned a badge
      Week One Done
    • First Post
      Ben Gross earned a badge
      First Post
    • One Month Later
      chiptuning earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      636
    2. 2
      +FloatingFatMan
      178
    3. 3
      ATLien_0
      144
    4. 4
      Xenon
      120
    5. 5
      wakjak
      108
  • Tell a friend

    Love Neowin? Tell a friend!