Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取当前URL并更改参数PHP_Php_Url_Request Uri - Fatal编程技术网

如何获取当前URL并更改参数PHP

如何获取当前URL并更改参数PHP,php,url,request-uri,Php,Url,Request Uri,我有这样一个URL: muliba/?i=page.alama&pro=as <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"]."&p

我有这样一个URL:

muliba/?i=page.alama&pro=as
<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>
我创建了这样一个链接:

muliba/?i=page.alama&pro=as
<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>
我怎样才能得到这样的结果

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

根据本文中的答案和对答案的修改,我将两个答案合并,以使您更简单。只需复制下面的代码并将其粘贴到您的文件中即可

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;
}
用法



特别感谢&

几年前我写了一篇文章,可能会帮助它替换部分查询字符串,如您需要的内容,例如
merge\u querystring($url',?pro=as')
这是否回答了您的问题?对不起,我还是不明白。。请帮我拿那个箱子。。因为我的链接不同。那么,当您尝试
merge\u querystring('http://'。$\u SERVER['http\u HOST']。$\u SERVER['REQUEST\u URI'],'pro=as')
时会发生什么?这与我链接的线程的用例完全相同。感谢大家,我已经尝试了以下代码,$url=“http://”。$\u SERVER['http\u HOST']。$\u SERVER['REQUEST\u URI']$dis_url=trim(strtok($url,&');它是有效的。