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!
-
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?user895378– user8953782012年02月17日 18:13:39 +00:00Commented Feb 17, 2012 at 18:13
5 Answers 5
if ( isset($_GET['s']) && count($_GET) > 1 ) {
header('Location: ?s='. rawurlencode($_GET['s']));
exit;
}
Edited to somewhat appease Jon.
-
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.Jon– Jon2012年02月17日 18:26:07 +00:00Commented Feb 17, 2012 at 18:26
-
2If this is what you needed, you should have specified it was the current URL and not implying it's a string like you have aboveMike– Mike2012年02月17日 18:26:54 +00:00Commented Feb 17, 2012 at 18:26
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.
-
I went for that in my answer! Don´t think rawurlencode() is required in my case, am I right ?Guilherme Viebig– Guilherme Viebig2012年02月17日 18:23:34 +00:00Commented 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.Jon– Jon2012年02月17日 18:27:50 +00:00Commented Feb 17, 2012 at 18:27
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!
-
this is flawless, and plus you can manipulate everything else that you need toGuilherme Viebig– Guilherme Viebig2012年02月17日 18:28:23 +00:00Commented 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 thatparse_url
would need to be verbose, I wasn't joking. :)Jon– Jon2012年02月17日 18:33:20 +00:00Commented 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.Guilherme Viebig– Guilherme Viebig2012年02月17日 18:53:37 +00:00Commented 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 thatparse_url
is more trouble than it's worth.Jon– Jon2012年02月17日 19:11:05 +00:00Commented Feb 17, 2012 at 19:11
parse_str(parse_url($url, PHP_URL_QUERY), $query);
$newurl = 'http://myurl.com/locations/?s=' . $query['s'];
-
1
-
@GuilhermeViebig Yes. Jon mentioned that 3 minutes ago.Mike– Mike2012年02月17日 18:33:42 +00:00Commented Feb 17, 2012 at 18:33
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'];
-
1What 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 ...user895378– user8953782012年02月17日 18:16:01 +00:00Commented 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.)simshaun– simshaun2012年02月17日 18:16:47 +00:00Commented Feb 17, 2012 at 18:16