search
Homephp教程php手册检查url链接是否已经有参数的php代码 添加 ? 或 &

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

Jun 13, 2016 pm 12:18 PM
ampphpurlcodePaginationparameterhaveexamineAdd toofLink

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

方法很简单:

复制代码 代码如下:

((strpos($url, '?') !== false) ? '&' : '?');



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

如果链接中没有参数,如:
http://www.test.com/index.php
则需要添加 & 然后跟上分页信息:
http://www.jb51.net/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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools