title: better way to replace query string value in a given url
body: <p>Okay.. so basically, say we have a link:</p>
  
  <pre><code>$url = "http://www.site.com/index.php?sub=Mawson&amp;state=QLD&amp;cat=4&amp;page=2&amp;sort=z";
  </code></pre>
  
  <p>Basically, I need to create a function, which replaces each thing in the URL, for example:</p>
  
  <pre><code>&lt;a href="&lt;?=$url;?&gt;?sort=a"&gt;Sort by A-Z&lt;/a&gt;
  &lt;a href="&lt;?=$url;?&gt;?sort=z"&gt;Sort by Z-A&lt;/a&gt;
  </code></pre>
  
  <p>Or, for another example:</p>
  
  <pre><code>&lt;a href="&lt;?=$url;?&gt;?cat=1"&gt;Category 1&lt;/a&gt;
  &lt;a href="&lt;?=$url;?&gt;?cat=2"&gt;Category 2&lt;/a&gt;
  </code></pre>
  
  <p>Or, another example:</p>
  
  <pre><code>&lt;a href="&lt;?=$url;?&gt;?page=1"&gt;1&lt;/a&gt;
  &lt;a href="&lt;?=$url;?&gt;?page=2"&gt;2&lt;/a&gt;
  &lt;a href="&lt;?=$url;?&gt;?page=3"&gt;3&lt;/a&gt;
  &lt;a href="&lt;?=$url;?&gt;?page=4"&gt;4&lt;/a&gt;
  </code></pre>
  
  <p>So basically, we need a function which will replace the specific <code>$_GET</code> from the URL so that we don't get a duplicate, such as: <code>?page=2&amp;page=3</code></p>
  
  <p>Having said that, it needs to be smart, so it knows if the beginning of the parameter is a <code>?</code> or an <code>&amp;</code></p>
  
  <p>We also need it to be smart so that we can have the URL like so:</p>
  
  <pre><code>&lt;a href="&lt;?=$url;?&gt;page=3"&gt;3&lt;/a&gt; (without the ? - so it will detect automatically wether to use an `&amp;` or a `?`
  </code></pre>
  
  <p>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.</p>
  
  <p>Thank you.</p>