tags:

views:

116

answers:

8

Is there a way I can strip out the variables from a link using PHP for example, if I have a link that reads http://localhost/link/index.php?s=30&p=3 how would I strip out ?s=30&p=3 so my link reads http://localhost/link/index.php

+2  A: 

Try this:

$pos = strpos($url, '?');
if($pos !== FALSE) {
   $url = substr($url, 0, $pos);
}
Jacob Relkin
Doesn't consider the case where `?` doesn't appear. "If length is given and is 0, **FALSE** or NULL an empty string will be returned [from substr)."
Artefacto
@Artefacto, now it does :)
Jacob Relkin
A: 
$url = 'http://localhost/link/index.php?s=30&p=3';
$d = explode("?", $url);
$stripped = reset($d);
Artefacto
Three lines instead of one, and probably quite a bit slower than Jacob’s suggestion.
Scytale
Does explode automatically delimit on '?' ? actually - you just seem to b using reset. oh, I see - I think that should read $stripped = reset($d);
Aidan Kane
@scy Jacob's solution doesn't work when there's no query string and these "3 lines" are actually two (one is an input definition) and could easily be 1 with trunk's array dereferencing.
Artefacto
@Aidan Fixed...
Artefacto
Your answer still hasn't been updated as far as I can see. I prefer this method myself.
Aidan Kane
A: 

I haven't tested this but you could probably do something like:

$my_url = 'http://localhost/link/index.php?s=30&p=3';
$split = explode('?', $my_url);
$new_url = $split[0];
Aidan Kane
Since when can you omit the `$` in php :) ?
svens
heh. oh yeah, oops. don't write much php anymore. I've updated so as to not cause confusion. thanks.
Aidan Kane
+7  A: 
list($url) = explode("?", $longUrl, 2);
svens
This seems to be the mother of all solutions to this questions. One line, not too slow and works even without a question mark present. Nice.
Scytale
A: 
$fullUrl = "http://localhost/link/index.php?s=30&p=3#dontreplace";
$urlData = parse_url($fullUrl);
$url = str_replace('?'.$urlData['query'],'',$fullUrl);

This solution takes into account that you could have a hashtag after the paremeters and it does not replace it.

If you just don't care of what is after the ? then use the other answers

Lombo
+5  A: 

Edit (suggested by Hoohah):

Also you can use strstr() (PHP 5.3.0 onward):

echo strstr($longurl, "?", TRUE);

PHP has a built in function for this.

It is parse_url(). Take a look at the possible return values. In this case we can use scheme, host, and path.

For example:

<?php
$info = parse_url("http://localhost/link/index.php?s=30&amp;p=3");
echo $info["scheme"] . "://" . $info["host"] . $info["path"];
  // Output: http://localhost/link/index.php
?>

Live example


The advantage of this method over using explode() is that it gives you control over whether you want to show the username, password, and port if included. The code above will not show any of these, so http://user:pass@localhost:81/link/index.php?s=30&amp;p=3 will return http://localhost/link/index.php, stripping the username, password, and port number, which is what I assume you'd want. Username, password, and port are available as $info["user"], $info["pass"], and $info["port"].

The explode() method fails if the password contains question marks. This method doesn't fail even with ? and @ signs in the password.


As a final note, if you are going to be dealing with port numbers, usernames, and passwords, you can use the code below (which has one added line) to strip usernames and passwords but keep port number:

<?php      
$info = parse_url("http://user:__?**@@@@&amp;?ss@localhost:80/link/index.php?s=30&amp;p=3");
  // If port is present add a colon before it, if not make it an empty string.
isset($info["port"]) ? $port = ":" . $info["port"] : $port ="";
echo $info["scheme"] . "://" . $info["host"] . $port . $info["path"];
  // Outputs: http://localhost:80/link/index.php
?>

Live example


Finally, you really should not be using username and password in the link. From RFC2396

Some URL schemes use the format "user:password" in the userinfo field. This practice is NOT RECOMMENDED, because the passing of authentication information in clear text (such as URI) has proven to be a security risk in almost every case where it has been used.

Peter Ajtai
Ouch. Hurts that you had to write the first sentence in bold. A bit embarrassing that we forgot parse_url() exists. It is by far the cleanest solution, yes.
Scytale
[Example of it failing](http://codepad.viper-7.com/tkmI8E). `parse_url` is not very adequate for this.
Artefacto
@Artefact - That's not a fail; that's a win. Do you **really** want the `username` and **`password`** included into HTML served???? This is exactly why `parse_url` is great. BTW if you want, you **can** include those when present.... just take a look at the possible return values.
Peter Ajtai
@Peter You can, but it's not so simple anymore. You have to check whether each one of those exist and then add them. And even if you ignore the username/password, you can't ignore the port. You have to add `. empty($info['port'])?":{$info['port']}":"" . `.
Artefacto
@Artefacto - Yes it's a pain, requiring extra lines; however, it can be done. The `explode()` method fails if the password contains one or more `?` s. ======> http://codepad.viper-7.com/WqX1GW
Peter Ajtai
Artefacto
@Artefacto - Can you quote the part you're referring to? I looked, but can't see it. I don't see how what you quote above restricts question marks? Also, why rely on some spec that may not be implemented when parse_url handles both `?` and `@` in the password gracefully?
Peter Ajtai
@Peter I quoted the relevant part: userinfo (username+password) can only contain those characters. Look into the RFC for the definition of unreserved and escape (none include the `?`). The "some spec" you're referring to is as official as you'll get. If there's a `?` or a `@` the string, it's not a valid URL.
Artefacto
@Artefacto - Thanks.
Peter Ajtai
A: 
parse_url('http://localhost/link/index.php?s=30&amp;p=3',PHP_URL_PATH);
stillstanding
This doesn't work (PARSE_URL_PATH is not even a constant, and if PHP_URL_PATH was used it would only give the path portion), but somehow it got upvoted twice.
Artefacto
A: 
$url = strtok($url,"?");
Col. Shrapnel