• 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

    • I value a game as a whole including graphics. But going backwards in terms of graphics with a game I'm not already invested in (as in something I played and loved in the past and still do) is off-putting. Its a very poor demo trailer if they didn't crank the settings to max that would be ridiculous. Different strokes for different folks i guess, i'm not into slop the trailer and likely game could have been far better, it doesn't it instead looks like it was made on a shoe string budget or with lack of experience. But that could also be down to trying to get it running on low budget/outdated hardware and sacrificing the top end.
    • > Our goal is to ensure that the App Store remains an outstanding opportunity for developers and a safe, trusted experience for our users. There are so many scam apps on the platform that it is hard to believe they are truly interested in having a safe, trusted experience for their users.
    • Google announces upgraded Gemini 2.5 Pro model with enhanced capabilities by Pradeep Viswanathan Google today announced an updated Gemini 2.5 model with several improvements that can be observed in popular AI benchmarks. Google particularly highlighted that the new Gemini 2.5 Preview 06-05 "Thinking" model performs better in coding, math, science, and reasoning. Last month, during Google I/O, Google released an updated version of the Gemini 2.5 Pro model with significant improvements. Today's update builds on the release of the previous month with further enhancements. In addition to benchmark improvements, based on user feedback, this updated Gemini 2.5 Pro model also comes with improved style and structure, which will result in creative and better-formatted responses for end users. You can find the benchmark comparison with other leading AI models below. As you can notice in the above table, this updated Gemini 2.5 Pro preview model is now SOTA (State-of-the-Art) in coding benchmarks like Aider Polyglot. It also scored SOTA performance on GPQA and Humanity’s Last Exam (HLE) benchmarks, which test math, science, knowledge, and reasoning capabilities. In real-world testing, this latest 2.5 Pro model scored 24 points better on LMArena, maintaining its lead, and a 35-point jump on WebDevArena at 1443. Developers can access this latest Gemini 2.5 Pro preview model through the Gemini API via Google AI Studio and Vertex AI. General consumers will be able to access this model via the Gemini app. Google also confirmed that the Gemini 2.5 Pro model will be generally available in a couple of weeks, allowing developers to start using it in production-ready enterprise-scale applications.
    • The Bromance is definitely over! 🤣🤣🤣
    • I have the chance to live where electricity comes only from dams.
  • Recent Achievements

    • Week One Done
      jbatch earned a badge
      Week One Done
    • First Post
      Yianis earned a badge
      First Post
    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      418
    2. 2
      +FloatingFatMan
      181
    3. 3
      snowy owl
      178
    4. 4
      ATLien_0
      174
    5. 5
      Xenon
      135
  • Tell a friend

    Love Neowin? Tell a friend!