0

所以我试图从数组中预定义的 URL 查询字符串中删除特定参数。这是我到目前为止所拥有的:

<?php
// Construct the current page URL
$host     = $_SERVER['HTTP_HOST'];
$script   = $_SERVER['SCRIPT_NAME'];
$params   = $_SERVER['QUERY_STRING'];
$currentUrl = 'http://' . $host . $script . '?' . $params;

// Store all URL parameters into an array (HOST, PATH, QUERY, etc)
$url_params = array();
$url_params = parse_url($currentUrl);

// Create an array to store just the query string, breaking them apart
$params_array = explode('&', $url_params['query']);

// Array holding URL parameters that we want to remove
$params_to_remove = array("param1", "param2", "param3", "param4", "param5");


$location = 0;
// Loop through and remove parameters found in PARAMS_TO_REMOVE array
for($x = 0; $x < count($params_to_remove); $x++) {
    if(in_array($params_to_remove[$x], $params_array)) {
        $location = array_search($params_to_remove[$x], $params_array);
        unset($params_array[$location]);
    }
}

// Print array after unwanted parameters were removed
print_r($params_array);
echo '<br /><br />';

// Construct a new array holding only the parameters that we want
$clean_params_array = array();
for($z = 0; $z < count($params_array); $z++) {
    if($params_array[$z] != '') array_push($clean_params_array, $params_array[$z]);
}

// Print clean array
print_r($clean_params_array);
echo '<br />';

// Construct the new URL
// If there are paramters remaining in URL reconstruct them
if(count($clean_params_array) > 0) {
    $final_url = 'http://www.example.com' . $url_params['path'] . '?';
    for($y = 0; $y < count($clean_params_array); $y++) {
        $final_url .= $clean_params_array[$y] . '&';
    }
    // Trim off the final ampersand
    $final_url = substr($final_url, 0, -1);
}
// No parameters in final URL
else $final_url = 'http://www.example.com' . $url_params['path'];

// Print final URL
echo '<br />' . $final_url;
?>

这是输出:

使用http://www.example.com/test.php?apple&banana&orange¶m1&strawberry¶m2&pineapple

Array ( [0] => apple [1] => banana [2] => orange [4] => strawberry [6] => pineapple ) 

Array ( [0] => apple [1] => banana [2] => orange [3] => strawberry ) 

http://www.example.com/test.php?apple&banana&orange&strawberry

如您所见,我丢失了最后一个参数。我也觉得好像我太啰嗦了……我哪里错了?

4

3 回答 3

3
$new_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']."?".implode("&",array_diff(explode("&",$_SERVER['QUERY_STRING']),Array("param1","param2","param3","param4","param5")));

单线;)

尽管您最好将其Array(...)从那里取出并事先将其定义为变量,这样更容易阅读。

于 2012-04-24T20:16:16.420 回答
2
<?php 
 /** 
  * generateURL() 
  * 
  * Sprava URL adresy 
  * 
  * @author   stenley <stenley@webdev.sk> 
  * @version   1.4 
  */ 

 function generateURL() { 
    $GET = $_GET; 
    $QUERY_STRING = ''; 
    $SCRIPT_NAME = substr(strrchr($_SERVER["SCRIPT_NAME"],"/"),1); 

    $num_args = func_num_args(); 
    if($num_args>0 && $num_args%2==0) { 
       $args = func_get_args(); 

       foreach($args as $index => $paramName) { 
          $paramName = trim($paramName); 

          if($index%2==0 && !empty($paramName)) { 
             $paramValue = trim($args[$index+1]); 

             if(array_key_exists($paramName, $GET) && empty($paramValue)) { 
                unset($GET[$paramName]);    
             } elseif(!empty($paramValue)) { 
                $GET[$paramName] = $paramValue; 
             } 
          } 
       } 
    } 

    foreach($GET as $param => $value) { 
       $QUERY_STRING .= $param."=".$value."&amp;"; 
    } 

    return $SCRIPT_NAME.((empty($QUERY_STRING)) ? '' : "?".substr($QUERY_STRING,0,-5)); 
 } 
 ?>

这是管理 URL 地址的绝佳功能。使用很简单。这里有一些斯洛伐克语的例子。但我想你会理解代码示例。或者我会为你翻译

对不起我的英语不好

于 2012-04-24T20:21:47.323 回答
1

部分答案在这一行: Array ( [0] => apple [1] =>banana [2] => orange [4] => strawberry [6] => pineapple )

请注意 $params_array[5] 不存在。但是,当 $z==5 时,您尝试读取 $params_array[5] (在您的 while 循环中,您会遍历值 $z = 0; => $z < 6; (count($params_array))

您可以使用 Kolink 的解决方案,或者使用foreach循环遍历所有值:

foreach($params_array as $param) {
    if($param != '') array_push($clean_params_array, $param); 
}
于 2012-04-24T20:24:13.950 回答