[英]PHP Get URL without Query String - While Modifiying existing query
長期潛伏在這里的第一次海報:
我搜索過高和低,並試圖保持我的php腳本與原樣相同:
$url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
但是,當我回顯$ url時,我需要:
從www.example.com/?utm_source=etc
刪除字符串,刪除/?
之后的所有內容/?
在多個文件名上,例如www.example.com/page.htm/?utm_source=etc
www.example.com/page1.htm/?utm_source=etc
等
保留Google自定義搜索查詢字符串www.example.com/search.htm?q=term
保持其他幾個搜索字符串接近GSE字符串示例
我已經看過一些示例,但是如果不作大量更改,沒有一個對我有用,surley有一個更簡單的方法。 提前致謝
這將為您完成工作。 我為它使用了php regex和preg_replace()
預定義函數。
正則表達式(Fiddle Link )
/www(\.)[A-Za-z0-9]+\.\w+(\/)(\?)?((\w)+(\.)(\w)+)?((.)+)?/i
PHP示例
<?php
$input_line = 'www.example.com/?utm_source=etc'; //Input String
$replace_String = ''; //specify your replace string here
$regex = preg_replace("/www(\.)[A-Za-z0-9]+\.\w+(\/)(\?)?((\w)+(\.)(\w)+)?((.)+)?/i", $replace_String, $input_line);
print_r($regex); //this will print the output with replaced string
?>
您需要將url分成不同的部分,然后將其重新組合在一起-這是唯一的方法。
例如,如果您的網址是
$url = www.example.com/page1.htm/?utm_source=etc&some_key=some_key_value&someothekey=someotherid
$url_array = explode("?",$url);
$url_path = $url_array[0];
$url_params = explode("&",$url_array[1]);
$i = 0;
foreach($url_params as $url_param){ // Let's get specific url parameter and it's value
$i++;
$url_key = explode("=",$url_param)[0];
$url_value = explode("=",$url_param)[1];
$some_key = "some_key";
/*
* or use $i auto increment to find, relevant search parameter based on position e.g.
* if($i = 0){ // 0 is first etc.
* $some_key = $url_key;
* }
*/
// now that we have all keys - let's compare
if($url_key === $some_key){
$some_key_value .= $url_value; // Note the dot before ".=" so you can use it outside the loop, do the same if statements for any other keys you need
}
}
$new_url = $url_path."?".$some_key."=".$some_key_value;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.