Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

better way to replace query string value in a given url

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.

like image 718
Latox Avatar asked Sep 09 '11 02:09

Latox


People also ask

How can I append a query parameter to an existing URL?

append() The append() method of the URLSearchParams interface appends a specified key/value pair as a new search parameter. As shown in the example below, if the same key is appended multiple times it will appear in the parameter string multiple times for each value.

How can I add or update a query string parameter?

You can use the browser's native URL API to do this in a fairly simple way, where key and value are your parameter name and parameter value respectively. const url = new URL(location. href); url. searchParams.


2 Answers

Well, I had same problem, found this question, and, in the end, prefered my own method. Maybe it has flaws, then please tell me what are they. My solution is:

$query=$_GET;
$query['YOUR_NAME']=$YOUR_VAL;
$url=$_SERVER['PHP_SELF']. '?' .  http_build_query($query);

Hope it helps.

like image 149
Amantel Avatar answered Oct 25 '22 00:10

Amantel


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>
like image 39
Scuzzy Avatar answered Oct 25 '22 00:10

Scuzzy