Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7871823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:01:23+00:00 2026-06-03T02:01:23+00:00

So I’m attempting to remove specific parameters from the URL query string that are

  • 0

So I’m attempting to remove specific parameters from the URL query string that are predefined in an array. Here’s what I have so far:

<?php
// Construct the current page URL
$host     = $_SERVER['HTTP_HOST'];
$script   = $_SERVER['SCRIPT_NAME'];
$params   = $_SERVER['QUERY_STRING'];
$currentUrl = 'http://' . $host . $script . '?' . $params;

// Store all URL parameters into an array (HOST, PATH, QUERY, etc)
$url_params = array();
$url_params = parse_url($currentUrl);

// Create an array to store just the query string, breaking them apart
$params_array = explode('&', $url_params['query']);

// Array holding URL parameters that we want to remove
$params_to_remove = array("param1", "param2", "param3", "param4", "param5");


$location = 0;
// Loop through and remove parameters found in PARAMS_TO_REMOVE array
for($x = 0; $x < count($params_to_remove); $x++) {
    if(in_array($params_to_remove[$x], $params_array)) {
        $location = array_search($params_to_remove[$x], $params_array);
        unset($params_array[$location]);
    }
}

// Print array after unwanted parameters were removed
print_r($params_array);
echo '<br /><br />';

// Construct a new array holding only the parameters that we want
$clean_params_array = array();
for($z = 0; $z < count($params_array); $z++) {
    if($params_array[$z] != '') array_push($clean_params_array, $params_array[$z]);
}

// Print clean array
print_r($clean_params_array);
echo '<br />';

// Construct the new URL
// If there are paramters remaining in URL reconstruct them
if(count($clean_params_array) > 0) {
    $final_url = 'http://www.example.com' . $url_params['path'] . '?';
    for($y = 0; $y < count($clean_params_array); $y++) {
        $final_url .= $clean_params_array[$y] . '&';
    }
    // Trim off the final ampersand
    $final_url = substr($final_url, 0, -1);
}
// No parameters in final URL
else $final_url = 'http://www.example.com' . $url_params['path'];

// Print final URL
echo '<br />' . $final_url;
?>

Here’s the output:

Using http://www.example.com/test.php?apple&banana&orange&param1&strawberry&param2&pineapple

Array ( [0] => apple [1] => banana [2] => orange [4] => strawberry [6] => pineapple ) 

Array ( [0] => apple [1] => banana [2] => orange [3] => strawberry ) 

http://www.example.com/test.php?apple&banana&orange&strawberry

As you can see I’m losing the last parameter. I also feel as if I’m being too verbose…where am I going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:01:25+00:00Added an answer on June 3, 2026 at 2:01 am
    $new_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']."?".implode("&",array_diff(explode("&",$_SERVER['QUERY_STRING']),Array("param1","param2","param3","param4","param5")));
    

    One-liner 😉

    Although you’d probably be better off taking that Array(...) out of there and defining it as a variable beforehand, so it’s easier to read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.