2

I have a URL like this:

muliba/?i=page.alama&pro=as

And I created a link like this:

<a href="<?php echo http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"]."&pro=as";?>">AS</a>
<a href="<?php echo http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"]."&pro=en";?>">EN</a>

If I open the link, the parameter is added next to already defined one like so:

muliba/?i=page.alama&pro=as&pro=en

How can I get result like this?

muliba/?i=page.alama&pro=as
muliba/?i=page.alama&pro=en

Thanks

richardev
  • 976
  • 1
  • 10
  • 33
barhea
  • 29
  • 4
  • I wrote something a few years ago that might help https://stackoverflow.com/questions/7356555 It will replace parts of the query string like what you need eg `merge_querystring($url,'?pro=as')` – Scuzzy Jun 15 '20 at 02:09
  • Does this answer your question? [better way to replace query string value in a given url](https://stackoverflow.com/questions/7356555/better-way-to-replace-query-string-value-in-a-given-url) – Scuzzy Jun 15 '20 at 02:15
  • Im sorry, I still don't understand.. please help me for that case.. cause my link is different.. – barhea Jun 15 '20 at 03:53
  • Well what happens when you try `merge_querystring( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], '?pro=as' )` ? It's the exact same use case as the thread I've linked. – Scuzzy Jun 15 '20 at 07:49

1 Answers1

1

According to Scuzzy answer and jlcolon13 Modifications on Scuzzy answer in this Question I merge both answers to make it simpler for you. Just Copy code below and paste it into your file!.

function merge_querystring($url = null,$query = null,$recursive = false){
  // $url = 'https://www.google.com?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_filter(array_merge_recursive($original_query_string, $merged_query_string));
} else {
    $merged_result = array_filter(array_merge($original_query_string, $merged_query_string));
}

// Find the original query string in the URL and replace it with the new one
$new_url = str_replace($url_components['query'], http_build_query($merged_result), $url);

// If the last query string removed then remove ? from url 
if(substr($new_url, -1) == '?') {
   return rtrim($new_url,'?');
}
return $new_url;
}

Usage

<a href="<?=merge_querystring($url,'?pro=en');?>">EN</a>
<a href="<?=merge_querystring($url,'?pro=as');?>">AS</a>

Special thanks to Scuzzy & jlcolon13

Amaan warsi
  • 184
  • 4
  • 18
  • Thanks for all, I've tried this code, $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; $dis_url=trim(strtok($url, '&')); and it works. – barhea Jun 18 '20 at 01:18