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 6544465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:28:07+00:00 2026-05-25T11:28:07+00:00

Okay.. so basically, say we have a link: $url = http://www.site.com/index.php?sub=Mawson&state=QLD&cat=4&page=2&sort=z; Basically, I need

  • 0

Okay.. so basically, say we have a link:

$url = "http://www.site.com/index.php?sub=Mawson&state=QLD&cat=4&page=2&sort=z";

Basically, I need to create a function, which replaces each thing in the URL, for example:

<a href="<?=$url;?>?sort=a">Sort by A-Z</a>
<a href="<?=$url;?>?sort=z">Sort by Z-A</a>

Or, for another example:

<a href="<?=$url;?>?cat=1">Category 1</a>
<a href="<?=$url;?>?cat=2">Category 2</a>

Or, another example:

<a href="<?=$url;?>?page=1">1</a>
<a href="<?=$url;?>?page=2">2</a>
<a href="<?=$url;?>?page=3">3</a>
<a href="<?=$url;?>?page=4">4</a>

So basically, we need a function which will replace the specific $_GET from the URL so that we don’t get a duplicate, such as: ?page=2&page=3

Having said that, it needs to be smart, so it knows if the beginning of the parameter is a ? or an &

We also need it to be smart so that we can have the URL like so:

<a href="<?=$url;?>page=3">3</a> (without the ? - so it will detect automatically wether to use an `&` or a `?`

I don’t mind making different variables for each preg_replace for the certain $_GET parameters, but I am looking for the best way to do this.

Thank you.

  • 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-05-25T11:28:08+00:00Added an answer on May 25, 2026 at 11:28 am

    How about something like this?

    function merge_querystring($url = null,$query = null,$recursive = false)
    {
      // $url = 'http://www.google.com.au?q=apple&type=keyword';
      // $query = '?q=banana';
      // if there's a URL missing or no query string, return
      if($url == null)
        return false;
      if($query == null)
        return $url;
      // split the url into it's components
      $url_components = parse_url($url);
      // if we have the query string but no query on the original url
      // just return the URL + query string
      if(empty($url_components['query']))
        return $url.'?'.ltrim($query,'?');
      // turn the url's query string into an array
      parse_str($url_components['query'],$original_query_string);
      // turn the query string into an array
      parse_str(parse_url($query,PHP_URL_QUERY),$merged_query_string);
      // merge the query string
      if($recursive == true)
        $merged_result = array_merge_recursive($original_query_string,$merged_query_string);
      else
        $merged_result = array_merge($original_query_string,$merged_query_string);
      // Find the original query string in the URL and replace it with the new one
      return str_replace($url_components['query'],http_build_query($merged_result),$url);
    }
    

    usage…

    <a href="<?=merge_querystring($url,'?page=1');?>">Page 1</a>
    <a href="<?=merge_querystring($url,'?page=2');?>">Page 2</a>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my code: http://pastebin.com/y0VHgRNs Okay Basically, What i need to do is make
Okay, so basically lets say i have a matrix: matrix([[0, 1, 2, 3, 4],
Okay basically here's where I'm at. I have a list of PropertyDescriptor objects. These
Okay, basically, I have a large list of phone numbers in a text file
Okay, so I have my post.php page. If you are not logged in, you'll
I'm wondering if it's considered okay (particularly, in Django) to have a URL that's
Okay,I've been following this tutorial http://coenraets.org/blog/android-samples/androidtutorial/ Basically it gives me exactly what i need
Okay so basically : i have this simple example: main.cpp using namespace VHGO::Resource; std::list<BaseTable*>
OKay, I will try to make this make sense lol. Basically I have 4
Okay, basically, I have formed a mySQL query which returns a data set all

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.