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

Questions:

i have this URI.

http://localhost/index.php?properties&status=av&page=1

i am fetching basename of the URI using following code.

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

the above code gives me following string.

index.php?properties&status=av&page=1

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

unset($queryParams['page']);

and rebuild the url

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

Answer:

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);

Answer:

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);
    unset($arrayUri[$variable]);
    $newUri = http_build_query($arrayUri);
    $newUri = $parseUri['path'].'?'.$newUri;
    return $newUri;
}

now consider the following uri

index.php?properties&status=av&page=1

//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 🙂

Answer:

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

Documentation for strrpos.

Answer:

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

$regex = '/(&|(?<=\?))page=.*?(?=&|$)/';
$urls = array(
        'index.php?properties&status=av&page=1',
        'index.php?properties&page=1&status=av',
        'index.php?page=1',
);
foreach($urls as $url) {
        echo preg_replace($regex, '', $url), "\n";
}

Output:

index.php?properties&status=av
index.php?properties&status=av
index.php?

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)

Answer:

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:

<?php
//$url="http://localhost/index.php?properties&status=av&page=1";
$base=basename($_SERVER['REQUEST_URI']);

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

//Find the last ampersand
$lastAmp=strrpos($base,"&");

//Filter, catch no ampersands found
$removeLast=($lastAmp===false?$base:substr($base,0,$lastAmp));

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.