12

I have a page that lists out items according to numerous parameters ie variables with values.

listitems.php?color=green&size=small&cat=pants&pagenum=1 etc.

To enable editing of the list, I have a parameter edit=1 which is appended to the above querystring to give:

listitems.php?color=green&size=small&cat=pants&pagenum=1&edit=1

So far so good.

WHen the user is done editing, I have a link that exits edit mode. I want this link to specify the whole querystring--whatever it may be as this is subject to user choices--except remove the edit=1.

When I had only a few variables, I just listed them out manually in the link but now that there are more, I would like to be able programmatically to just remove the edit=1.

Should I do some sort of a search for edit=1 and then just replace it with nothing?

$qs = str_replace("&edit=1, "", $_SERVER['QUERY_STRING']);
<a href='{$_SERVER['PHP_SELF']}?{$qs}'>return</a>;

Or what would be the cleanest most error-free way to do this.

Note: I have a similar situation when going from page to page where I'd like to take out the pagenum and replace it with a different one. There, since the pagenum varies, I cannot just search for pagenum=1 but would have to search for pagenum =$pagenum if that makes any difference.

Thanks.

asked Jun 15, 2013 at 10:20

4 Answers 4

26

I'd use http_build_query, which nicely accepts an array of parameters and formats it correctly. You'd be able to unset the edit parameter from $_GET and push the rest of it into this function.

Note that your code has a missing call to htmlspecialchars(). A URL can contain characters that are active in HTML. So when outputting it into a link: Escape!

Some example:

unset($_GET['edit']); // delete edit parameter;
$_GET['pagenum'] = 5; // change page number
$qs = http_build_query($_GET);
... output link here.
answered Jun 15, 2013 at 10:30
3
  • 1
    How would I do this from $_SERVER['QUERY_STRING']? Commented Jun 15, 2013 at 10:35
  • There is parse_url() and parse_str(), but $_GET is always available and filled, so this is not necessary. Commented Jun 15, 2013 at 10:38
  • 4
    I wouldn't modify the $_GET variable (or any superglobals for that matter) because they could be needed elsewhere in the application. Instead, create a temporary variable like $edit_query = $_GET;. Then you can unset($edit_query['edit']); without affecting your superglobals. Commented Dec 14, 2017 at 20:30
9

Here's my shot:

/**
* Receives a URL string and a query string to remove. Returns URL without the query string
*/
function remove_url_query($url, $key) {
 $url = preg_replace('/(?:&|(\?))' . $key . '=[^&]*(?(1)&|)?/i', "1ドル", $url);
 $url = rtrim($url, '?');
 $url = rtrim($url, '&');
 return $url;
}

Returns:

remove_url_query('http://example.com?a', 'a') => http://example.com
remove_url_query('http://example.com?a=1', 'a') => http:/example.com
remove_url_query('http://example.com?a=1&b=2', 'a') => http://example.com?b=2

Kudos to David Walsh.

answered Apr 28, 2018 at 1:07
3

Another solution, to avoid &amp; problems too!!!

remove_query('http://example.com/?a=valueWith**&amp;**inside&b=value');

Code:

function remove_query($url, $which_argument=false){ 
 return preg_replace('/'. ($which_argument ? '(\&|)'.$which_argument.'(\=(.*?)((?=&(?!amp\;))|$)|(.*?)\b)' : '(\?.*)').'/i' , '', $url); 
}
answered Nov 29, 2017 at 11:56
1

It wouldn't work if edit=1 is the first variable:

listitems.php?edit=1&color=green&...

You can use the $_GET variable to create the query string yourself. Something like:

$qs = '';
foreach ($_GET as $key => $value){
 if ($key == 'pagenum'){
 // Replace page number
 $qs .= $key . '=' . $new_page_num . '&';
 }elseif ($key != 'edit'){
 // Keep all key/values, except 'edit'
 $qs .= $key . '=' . urlencode($value) . '&';
 }
}
answered Jun 15, 2013 at 10:28
0

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.