开发者

PHP url question

Is there a way I can strip out th开发者_如何学JAVAe 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


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


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.


Try this:

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


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


$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


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


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜