有没有一种方法可以用PHP从链接中去掉变量?例如,如果我有一个http://localhost/link/index.php?s=30&p=3链接,我该如何去掉?s=30&p=3,这样我的链接就会显示http://localhost/link/index.php
发布于 2010-08-13 01:21:09
list($url) = explode("?", $longUrl, 2);发布于 2010-08-13 01:30:41
编辑(suggested by Hoohah):
你也可以使用strstr() (PHP5.3.0以后的版本):
echo strstr($longurl, "?", TRUE);PHP有一个内置的函数来处理这个问题。
它是。看看可能的返回值。在这种情况下,我们可以使用scheme、host和path。
例如:
<?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
?>与使用explode()相比,这种方法的优点在于,它允许您控制是否要显示用户名、密码和端口(如果包含)。上面的代码不会显示其中的任何内容,因此http://user:pass@localhost:81/link/index.php?s=30&p=3将返回http://localhost/link/index.php,去掉用户名、密码和端口号,我想这就是您想要的。用户名、密码和端口可用作$info["user"]、$info["pass"]和$info["port"]。
如果密码包含问号,则explode() 方法将失败。This method doesn't fail even with ? and @ signs in the password。
最后要注意的是,如果您要处理端口号、用户名和密码,则可以使用以下代码(添加了一行)来删除用户名和密码,但保留端口号:
<?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
?>最后,你真的不应该在链接中使用用户名和密码。来自
某些网址方案在用户信息字段中使用"user:password“格式。不推荐使用这种做法,因为以明文形式传递身份验证信息(例如URI)已被证明在几乎所有使用它的情况下都存在安全风险。
发布于 2010-08-13 01:12:03
试试这个:
$pos = strpos($url, '?');
if($pos !== FALSE) {
$url = substr($url, 0, $pos);
}https://stackoverflow.com/questions/3470227
复制相似问题