Long time lurker first time poster here:
I have searched high and low and am trying to keep my php script somewhat the same as it is:
$url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
however when I echo $url I need to:
Remove the string from www.example.com/?utm_source=etc
removing everything after /?
on multiple file names like www.example.com/page.htm/?utm_source=etc
www.example.com/page1.htm/?utm_source=etc
and so on
Keep the google custom search query string www.example.com/search.htm?q=term
Keep a couple other search string close the GSE string example
Ive seen some examples but none worked for me without making a ton of changes, surley there is a easier way. Thanks in Advance
This will do the job for you. I used php regex and preg_replace()
predefined function for it.
Regex (Fiddle Link )
/www(\.)[A-Za-z0-9]+\.\w+(\/)(\?)?((\w)+(\.)(\w)+)?((.)+)?/i
Php example
<?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
?>
You need to split url into different pieces and then put all of it back together - this is the only way.
So for example if your url is
$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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.