using php parse_url function to remove the query string from a url
I recently had to remove the query string from a url, the bit after the ?key=1 or whatever is after the actual url.
I initially thought i could just use a explode function on the ? which would probably also work and then grab the 1st part of that array, but then i fund the function parse_url which automatically adds the url string into an array that you can access. I think this is probably a safer method of doing this, in case the url does not have a query string, or there is some other kind of error in the url.
Anyway here is how I used it.
$my_url = "https://kruxor.com/?moo=1"; /*we want to remove the ?moo=1 here */ $my_url_parse = parse_url($my_url); $my_new_url = $my_url_parse['scheme'] . "://" . $my_url_parse['host'] . $my_url_parse['path']; /*now we have to rebuild this string using parts of the parse array */ /* Lets see how it went... */ echo "<b>My Original URL</b><br>"; echo $my_url . "<br>"; echo "<b>My New URL</b><br>"; echo $my_new_url . "<br>";