亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何獲取當前 URL 并更改參數 PHP

如何獲取當前 URL 并更改參數 PHP

PHP
慕碼人2483693 2023-05-26 17:18:37
我有一個這樣的網址: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&pro=en我怎樣才能得到這樣的結果?muliba/?i=page.alama&pro=asmuliba/?i=page.alama&pro=en謝謝
查看完整描述

1 回答

?
飲歌長嘯

TA貢獻1951條經驗 獲得超3個贊

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;

}

用法


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

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


查看完整回答
反對 回復 2023-05-26
  • 1 回答
  • 0 關注
  • 143 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號