<?php
$URI_parts = explode('?', $_SERVER['REQUEST_URI'], 2);
echo 'http://' . $_SERVER['HTTP_HOST'] . $URI_parts[0];
?>
I've read that explode was rather heavy on resources, and I was curious if there's an optimal alternative that will perform the same task in this case.
The code above is destroying everything following a query string ? including parameters. I need to find the least costly method to do this.
You should use parse_url(), like this :
$URI_parts = parse_url( $_SERVER['REQUEST_URI'] );
echo 'http://' . $_SERVER['SERVER_NAME'] . $URI_parts['path'];
You could use strstr($_SERVER['REQUEST_URI'], "?", TRUE). Alternatively, you could use strtok($_SERVER['REQUEST_URI'], "?").
It should stop matching after it's satisfied.
As an aside, you shouldn't not use a PHP function because you heard [citation needed] that it was slow. You should use the appropriate tool for the job, and if that's too slow after finding a performance issue, measuring it and determining it's your use of that function, then consider refactoring.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With