Home > Backend Development > PHP Tutorial > PHP code Check whether the url link already has parameters PHP code Add ? or &

PHP code Check whether the url link already has parameters PHP code Add ? or &

WBOY
Release: 2016-07-29 08:41:48
Original
834 people have browsed it

For example, paging, because some links already have parameters, the original parameters cannot be discarded when appending paging information, so determine whether the link has parameters, and then append paging information as needed.
The method is very simple:

Copy the code The code is as follows:

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


Check whether the link is Contains ?, if so, such as:
http://www.test.com/index.php?id=id
Add an & directly after the link and then follow the paging information:
http://www.jb51. net/index.php?id=id&page=12
If there are no parameters in the link, such as:
http://www.test.com/index.php
You need to add & and then follow the paging information:
http:// www.jb51.net/index.php?page=12
Attached is a more robust checking method:

Copy the code The code is as follows:


$old_url = $_SERVER[" REQUEST_URI"];
//Check if the link exists?
$check = strpos($old_url, '?');
//If it exists?
if($check !== false)
{
//If? There are no parameters behind, such as http://www.yitu.org/index.php?
if(substr($old_url, $check+1) == '')
{
//You can add additional parameters directly
$ new_url = $old_url;
}
else //If there are parameters, such as: http://www.yitu.org/index.php?ID=12
{
$new_url = $old_url.'&';
}
}
else //If it does not exist?
{
$new_url = $old_url.'?';
}
echo $new_url;
?>

The above introduces the PHP code. Check whether the URL link already has PHP code with parameters. Add ? or &, which includes PHP code content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template