2

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

9 Answers 9

10
list($url) = explode("?", $longUrl, 2);
Sign up to request clarification or add additional context in comments.

1 Comment

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

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&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&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:__?**@@@@&?ss@localhost:80/link/index.php?s=30&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.

8 Comments

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.
Example of it failing. parse_url is not very adequate for this.
@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 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']}":"" . .
@Peter The password cannot question marks. Read RFC 2396. userinfo = *( unreserved | escaped | ";" | ":" | "&" | "=" | "+" | "$" | "," )
|
2

Try this:

$pos = strpos($url, '?');
if($pos !== FALSE) {
   $url = substr($url, 0, $pos);
}

1 Comment

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)."
0
$url = 'http://localhost/link/index.php?s=30&p=3';
$d = explode("?", $url);
$stripped = reset($d);

4 Comments

Three lines instead of one, and probably quite a bit slower than Jacob’s suggestion.
Does explode automatically delimit on '?' ? actually - you just seem to b using reset. oh, I see - I think that should read $stripped = reset($d);
@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.
Your answer still hasn't been updated as far as I can see. I prefer this method myself.
0
$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

Comments

0
$url = strtok($url,"?");

Comments

0

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];

2 Comments

Since when can you omit the $ in php :) ?
heh. oh yeah, oops. don't write much php anymore. I've updated so as to not cause confusion. thanks.
0
parse_url('http://localhost/link/index.php?s=30&p=3',PHP_URL_PATH);

1 Comment

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

If U need to parse the requested url, U can simply take it from globals:

// http://localhost/foo/bar?foo=bar&Foo1=Bar1
var_dump($_SERVER['QUERY_STRING']); // string(17) "foo=bar&Foo1=Bar1"

If symfony/http-foundation component is used, U can get query string from its Request class as follows:

$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
$queryString = $request->getQueryString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.