stacks
stacks

Reputation: 117

PHP url question

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

Upvotes: 2

Views: 1100

Answers (9)

Oleksii Zymovets
Oleksii Zymovets

Reputation: 740

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();

Upvotes: 0

Peter Ajtai
Peter Ajtai

Reputation: 57715

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.

Upvotes: 6

bcosca
bcosca

Reputation: 17555

parse_url('http://localhost/link/index.php?s=30&p=3',PHP_URL_PATH);

Upvotes: 0

Aidan Kane
Aidan Kane

Reputation: 4006

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

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 158007

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

Upvotes: 0

Lombo
Lombo

Reputation: 12235

$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

Upvotes: 0

svens
svens

Reputation: 11628

list($url) = explode("?", $longUrl, 2);

Upvotes: 10

Artefacto
Artefacto

Reputation: 97845

$url = 'http://localhost/link/index.php?s=30&p=3';
$d = explode("?", $url);
$stripped = reset($d);

Upvotes: 0

Jacob Relkin
Jacob Relkin

Reputation: 163318

Try this:

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

Upvotes: 2

Related Questions