0

Suppose I have the following, which is the current url:

http://myurl.com/locations/?s=bricks&style=funky-quirky+rustic&feature=floors-concrete+kitchen+bathroom

How would I remove all parameters except S using PHP? In other words, just leaving: http://myurl.com/locations/?s=bricks The other parameters will always be Style, Type, Feature, and Area, and some or all of them may be present.

Thanks!

asked Feb 17, 2012 at 18:09
1
  • In what context do you want to remove them? Are you just trying to replace one string with another? Or are you looking to redirect a request for the original to the shortend URI? Or does it even matter because you aren't required to use any of the extraneous parameters passed via GET in your script anyway? Commented Feb 17, 2012 at 18:13

5 Answers 5

1
if ( isset($_GET['s']) && count($_GET) > 1 ) {
 header('Location: ?s='. rawurlencode($_GET['s']));
 exit;
}

Edited to somewhat appease Jon.

answered Feb 17, 2012 at 18:14
2
  • Obvious but dubious as we say in chess. This will not work with any URL other than the current, and it can also result in a malformed URL because it does not urlencode the value. Commented Feb 17, 2012 at 18:26
  • 2
    If this is what you needed, you should have specified it was the current URL and not implying it's a string like you have above Commented Feb 17, 2012 at 18:26
1

Not much in the way of error checking, but if s is guaranteed to exist this will work:

list($base, $query) = explode('?', $url);
parse_str($query, $vars);
$result = $base.'?s='.rawurlencode($vars['s']);

It will also work no matter in what position s appears in the query string. I 'm doing a minimal initial "parsing" step with explode, but if that doesn't feel right you can always bring in the big guns and go with parse_url instead at the expense of being much more verbose.

answered Feb 17, 2012 at 18:14
2
  • I went for that in my answer! Don´t think rawurlencode() is required in my case, am I right ? Commented Feb 17, 2012 at 18:23
  • @GuilhermeViebig: It's required if you want the result to be valid in the same contexts where the original URL would also be valid (i.e. if you intend to give that URL to a browser). Although in many cases browsers and other software will detect and automagically correct this mistake, it still is a mistake. Commented Feb 17, 2012 at 18:27
1

You can achieve this in several different manners.

I will show you the best standard I ever faced to work with urls:

$originUrl = parse_url('http://myurl.com/locations/?s=bricks&style=funky-quirky+rustic&feature=floors-concrete+kitchen+bathroom'); 
parse_str($originUrl['query'], $queryString);
echo $newUrl = $originUrl['scheme'].'://'.$originUrl['host'].$originUrl['path'].'?s='.$queryString['s']; 

Good Luck!

answered Feb 17, 2012 at 18:21
4
  • this is flawless, and plus you can manipulate everything else that you need to Commented Feb 17, 2012 at 18:28
  • Sorry to say, but this is far from flawless. If you want to use parse_url then you have to assemble back all the pieces that may have been there. What if there's a user, a password, or a port specified? When I wrote in my answer that parse_url would need to be verbose, I wasn't joking. :) Commented Feb 17, 2012 at 18:33
  • @Jon If there´s a user/post or anything that isn´t listed, is just a matter of assembling it again, no big deal, some more variables. This is way faster than using array manipulations. But, dont say to me that using a PHP standard is worse than explode strings :> Like I said, this is the best standardized method I faced, thats is similar to yours, except from the parse_url. Commented Feb 17, 2012 at 18:53
  • I certainly did not say that using parse_url is worse. However, as a finished solution this would legitimately be worse because it does not work correctly in all cases -- we agree on this. My point is that whatever tool you pick, you have to use it correctly. As an aside, in this particular case I do think that parse_url is more trouble than it's worth. Commented Feb 17, 2012 at 19:11
1
parse_str(parse_url($url, PHP_URL_QUERY), $query);
$newurl = 'http://myurl.com/locations/?s=' . $query['s'];
answered Feb 17, 2012 at 18:23
2
  • 1
    There is no function parse_string in PHP, and parse_str (which you certainly intended instead) does not work that way as it does not return the result of the parse. Commented Feb 17, 2012 at 18:29
  • @GuilhermeViebig Yes. Jon mentioned that 3 minutes ago. Commented Feb 17, 2012 at 18:33
1

If you know that s will always be the first parameter, then you can simply do:

$url = "http://myurl.com/locations/?s=bricks&style=funky-quirky+rustic&feature=floors-concrete+kitchen+bathroom";
$split = explode('&',$url);
echo $split[0];

However, if the position of s in the parameters is unkown, then you can do this:

$url = "http://myurl.com/locations/?s=bricks&style=funky-quirky+rustic&feature=floors-concrete+kitchen+bathroom";
$split = explode('?',$url);
parse_str($split[1],$params);
echo $split[0].'?s='.$params['s'];
answered Feb 17, 2012 at 18:12
2
  • 1
    What if s isn't the first parameter passed via GET? The order doesn't matter when GET variables are used, so this isn't really sustainable ... Commented Feb 17, 2012 at 18:16
  • Assumes s is always the first parameter in the query string. (Just my opinion, but it shouldn't assume that.) Commented Feb 17, 2012 at 18:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.