Home  >  Article  >  Backend Development  >  检查url链接是否已经有参数的php代码 添加 ? 或 &_PHP

检查url链接是否已经有参数的php代码 添加 ? 或 &_PHP

WBOY
WBOYOriginal
2016-06-01 12:20:10865browse

比如分页,因为有些链接已经有参数了,在附加分页信息的时候不能把原有的参数丢掉,所以判断一下链接是否有参数,然后根据需要附加分页信息。

方法很简单:
复制代码 代码如下:((strpos($url, '?') !== false) ? '&' : '?');

检查链接中是否含有 ? ,如果有,如:
http://www.test.com/index.php?id=id
则直接在链接后面添加一个 & 然后跟上分页信息:
http://www.bitsCN.com/index.php?id=id&page=12

如果链接中没有参数,如:
http://www.test.com/index.php
则需要添加 & 然后跟上分页信息:
http://www.bitsCN.com/index.php?page=12

附上一个更为健全的检查方法:
复制代码 代码如下:
$old_url = $_SERVER["REQUEST_URI"];
//检查链接中是否存在 ?
$check = strpos($old_url, '?');
//如果存在 ?
if($check !== false)
{
//如果 ? 后面没有参数,如 http://www.yitu.org/index.php?
if(substr($old_url, $check+1) == '')
{
//可以直接加上附加参数
$new_url = $old_url;
}
else //如果有参数,如:http://www.yitu.org/index.php?ID=12
{
$new_url = $old_url.'&';
}
}
else //如果不存在 ?
{
$new_url = $old_url.'?';
}
echo $new_url;
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn