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?

5
  • What if it's index.php?properties&page=1&status=av instead? Do you want to remove page or status? Commented Aug 25, 2011 at 18:29
  • Take a look here, this may help you. stackoverflow.com/questions/1251582/… Commented Aug 25, 2011 at 18:29
  • @mercator i would want to remove page Commented Aug 25, 2011 at 18:35
  • if you're just doing pagination, check $_GET['page'] and increment it. Commented Aug 25, 2011 at 18:35
  • @dnagirl, sounds good but you are explaining out of context. i knew i could do that :) Commented Aug 25, 2011 at 18:50

6 Answers 6

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;
Sign up to request clarification or add additional context in comments.

3 Comments

this seriously looks good. and to be frank i was expecting to do something like this.
Very nice, worked perfectly for me as well! Thank you KingCrunch :)
You can also do parse_str(html_entity_decode($parts['query']), $queryParams); to preserve the & when echo'ing it out
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);

5 Comments

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.
preg_replace("/\&page=[0-9]+/i","",$myString); ... only the page :)
index.php?page=123&properties&status=av?
He wanted to match the &, so this was included ... but hell, I will change ;)
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.
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 :)

Comments

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

Documentation for strrpos.

6 Comments

I think you mean strrpos($_SERVER['REQUEST_URI'], '&')
although this will work but it will just remove the last Variable. i am sorry for not stating this before but i would like to trim $page=1 regardless of it's position it may occur before status=av or after.
You can do it with built in PHP functions, but it's better to use a regular expression in that case.
@root45, i am little weaker in regex. i would appreciate if you could help me on this :)
@root45 ... you could at least have the motivation to change my variable name before you 'rip off' my post directly below ;)
|
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)

3 Comments

i appreciate that @Gabi Purcaru, or how about the extended solution from KingCrunch, check my answer.
Unfortunately this causes problems in PHP < 5.2.2
Best options ! Thank you very much !
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.