PHP url问题

PHP url问题,php,Php,是否有一种方法可以使用PHP从链接中去除变量?例如,如果我有一个读取http://localhost/link/index.php?s=30&p=3我该如何去掉?s=30&p=3这样我的链接就会显示http://localhost/link/index.php试试这个: $pos = strpos($url, '?'); if($pos !== FALSE) { $url = substr($url, 0, $pos); } 我尚未对此进行测试,但您可能会执行以下操作: $my_url

是否有一种方法可以使用PHP从链接中去除变量?例如,如果我有一个读取
http://localhost/link/index.php?s=30&p=3
我该如何去掉
?s=30&p=3
这样我的链接就会显示
http://localhost/link/index.php

试试这个:

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

我尚未对此进行测试,但您可能会执行以下操作:

$my_url = 'http://localhost/link/index.php?s=30&p=3';
$split = explode('?', $my_url);
$new_url = $split[0];
这个解决方案考虑到在参数后面可能有一个hashtag,而不是替换它

如果你只是不在乎那之后是什么?然后使用其他答案编辑():

您还可以使用(PHP5.3.0以后的版本):


PHP有一个内置函数用于此操作。

是的。看看可能的返回值。在这种情况下,我们可以使用
方案
主机
路径

例如:

<?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
?>


最后,您真的不应该在链接中使用用户名和密码。从

一些URL方案使用userinfo中的格式“user:password” 领域不建议采用这种做法,因为 明文(如URI)中的身份验证信息已被证明是有效的 在几乎所有使用过它的情况下,它都是一种安全风险


如果您需要解析请求的url,只需从globals获取:

// http://localhost/foo/bar?foo=bar&Foo1=Bar1
var_dump($_SERVER['QUERY_STRING']); // string(17) "foo=bar&Foo1=Bar1"
如果使用symfony/http foundation组件,您可以从其请求类中获取查询字符串,如下所示:

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

不考虑<代码>?> /代码>没有出现的情况。“如果给定的长度为0,则会返回一个空字符串(来自substr)。”三行而不是一行,并且可能比Jacob的建议慢了一点。explode是否会自动在“?”上划界?实际上-您似乎只是在使用重置时使用了b。哦,我明白了-我认为应该是$stripped=reset($d);@scy Jacob的解决方案在没有查询字符串且这三行实际上是两行(一行是输入定义)的情况下不起作用在trunk的数组解引用中很容易得到1。据我所知,你的答案还没有更新。我自己更喜欢这种方法。从什么时候开始,你可以在php中省略
$
)?呵呵,是的,哎呀。不要再写太多php了。我已经更新了,以免引起混乱。谢谢。这似乎是所有解决方案的母亲问题。一行,不太慢,即使没有问号也能用。很好。哎哟。你不得不用粗体写第一句让人伤心。有点尴尬,我们忘了解析url()存在。这是迄今为止最干净的解决方案,是的。
parse_-url
不足以解决这个问题。@artifact-这不是失败;这是胜利。你真的想在HTML中包含
username
password
esent…看看可能的返回值。@Peter您可以,但它不再那么简单了。您必须检查它们是否存在,然后添加它们。即使忽略用户名/密码,也不能忽略端口。您必须添加
.empty($info['port'])?“:{$info['port']}”:”.
@Peter密码不能打问号。请阅读RFC 2396.userinfo=*(无保留的|转义|“;”;“|”:“|”&“|”=”|“+”|“$”,”)。这不起作用(解析| URL|路径甚至不是常量,如果使用PHP | URL|路径,它只会给出路径部分),但不知何故它被提升了两次。
echo strstr($longurl, "?", TRUE);
<?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
?>
<?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
?>
parse_url('http://localhost/link/index.php?s=30&p=3',PHP_URL_PATH);
$url = strtok($url,"?");
// http://localhost/foo/bar?foo=bar&Foo1=Bar1
var_dump($_SERVER['QUERY_STRING']); // string(17) "foo=bar&Foo1=Bar1"
$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
$queryString = $request->getQueryString();