7

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?

Ibrahim Azhar Armar
  • 25,288
  • 35
  • 131
  • 207

6 Answers6

20

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;
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
3

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);
ty812
  • 3,293
  • 19
  • 36
  • i appreciate your help and sorry for not mentioning this before but this will only remove the last variable from the string, wheras i am looking to remove the `&page=1` variable regardless of it's position. – Ibrahim Azhar Armar Aug 25 '11 at 18:45
  • 1
    preg_replace("/\&page=[0-9]+/i","",$myString); ... only the page :) – ty812 Aug 25 '11 at 18:47
  • `index.php?page=123&properties&status=av`? – KingCrunch Aug 25 '11 at 18:59
  • 1
    He wanted to match the &, so this was included ... but hell, I will change ;) – ty812 Aug 25 '11 at 19:03
  • `index.php?momomomooonsterpage=123&properties&status=av`? (=> `index.php?momomomooonster&properties&status=av`;)) Regular expressions have many pitfalls. I don't want to embarrass you, but such things often leads to weird behavior later, that are hard to track down. – KingCrunch Aug 25 '11 at 19:36
3

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

Ibrahim Azhar Armar
  • 25,288
  • 35
  • 131
  • 207
2
$pos = strrpos($_SERVER['REQUEST_URI'], '&');    
$url = substr($_SERVER['REQUEST_URI'], 0, $pos - 1);

Documentation for strrpos.

Kris Harper
  • 5,672
  • 8
  • 51
  • 96
2

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)
Gabi Purcaru
  • 30,940
  • 9
  • 79
  • 95
1

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.

Rudu
  • 15,682
  • 4
  • 47
  • 63