Home » Php » string – Function to remove GET variable with php

string – Function to remove GET variable with php

Posted by: admin July 12, 2020 Leave a comment


i have this URI.


i am fetching basename of the URI using following code.

$basename = basename($_SERVER['REQUEST_URI']);

the above code gives me following string.


i would want to remove the last variable from the string i.e &page=1. please note the value for page will not always be 1. keeping this in mind i would want to trim the variable this way.

Trim from the last position of the string till the first delimiter i.e &

Update :

I would like to remove &page=1 from the string, no matter in which position it is on.

how do i do this?

How to&Answers:

Instead of hacking around with regular expression you should parse the string as an url (what it is)

$string = 'index.php?properties&status=av&page=1';

$parts = parse_url($string);

$queryParams = array();
parse_str($parts['query'], $queryParams);

Now just remove the parameter


and rebuild the url

$queryString = http_build_query($queryParams);
$url = $parts['path'] . '?' . $queryString;


There are many roads that lead to Rome. I’d do it with a RegEx:

$myString = 'index.php?properties&status=av&page=1';
$myNewString = preg_replace("/\&[a-z0-9]+=[0-9]+$/i","",$myString);

if you only want the &page=1-type parameters, the last line would be

$myNewString = preg_replace("/\&page=[0-9]+/i","",$myString);

if you also want to get rid of the possibility that page is the only or first parameter:

$myNewString = preg_replace("/[\&]*page=[0-9]+/i","",$myString);


Thank you guys but i think i have found the better solution, @KingCrunch had suggested a solution i extended and converted it into function. the below function can possibly remove or unset any URI variable without any regex hacks being used. i am posting it as it might help someone.

function unset_uri_var($variable, $uri) {   
    $parseUri = parse_url($uri);
    $arrayUri = array();
    parse_str($parseUri['query'], $arrayUri);
    $newUri = http_build_query($arrayUri);
    $newUri = $parseUri['path'].'?'.$newUri;
    return $newUri;

now consider the following uri


//To remove properties variable
$url = unset_uri_var('properties', basename($_SERVER['REQUEST_URI']));
//Outputs index.php?page=1&status=av

//To remove page variable
$url = unset_uri_var('page', basename($_SERVER['REQUEST_URI']));
//Outputs index.php?properties=&status=av

hope this helps someone. and thank you @KingKrunch for your solution 🙂


$pos = strrpos($_SERVER['REQUEST_URI'], '&');    
$url = substr($_SERVER['REQUEST_URI'], 0, $pos - 1);

Documentation for strrpos.


Regex that works on every possible situation: /(&|(?<=\?))page=.*?(?=&|$)/. Here’s example code:

$regex = '/(&|(?<=\?))page=.*?(?=&|$)/';
$urls = array(
foreach($urls as $url) {
        echo preg_replace($regex, '', $url), "\n";



Regex explanation:

  • (&|(?<=\?)) — either match a & or a ?, but if it’s a ?, don’t put it in the match and just ignore it (you don’t want urls like index.php&status=av)
  • page=.*? — matches page=[...]
  • (?=&|$) — look for a & or the end of the string ($), but don’t include them for the replacement (this group helps the previous one find out exactly where to stop matching)


You could use a RegEx (as Chris suggests) but it’s not the most efficient solution (lots of overhead using that engine… it’s easy to do with some string parsing:


echo "Basename yields: $base<br />";

//Find the last ampersand

//Filter, catch no ampersands found

echo "Without Last Parameter: $removeLast<br />";

The trick is, can you guarantee that $page will be stuck on the end? If it is – great, if it isn’t… what you asked for may not always solve the problem.