使用PHP从URL中删除特定参数

时间:2012-04-24 20:09:44

标签: php arrays url

所以我试图从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&param1&strawberry&param2&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

如你所见,我失去了最后一个参数。我也觉得自己好像太啰嗦......我哪里出错?

3 个答案:

答案 0 :(得分:3)

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

One-liner;)

虽然您可能最好将Array(...)从那里取出并事先将其定义为变量,因此更容易阅读。

答案 1 :(得分: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地址的功能。用法很简单。斯洛伐克语here are some examples。但我想你会理解代码示例。或者我会为你翻译

  

抱歉我的英文

答案 2 :(得分:1)

部分答案在这一行:     数组([0] =&gt; apple [1] =&gt;香蕉[2] =&gt;橙色[4] =&gt;草莓[6] =&gt;菠萝)

请注意,$ params_array [5]不存在。然而,当$ z == 5时,你试图读取$ params_array [5] (在你的while循环中,你会看到值$ z = 0; =&gt; $ z&lt; 6;(count($ params_array))

您可以使用Kolink的解决方案,或使用foreach循环来浏览所有值:

foreach($params_array as $param) {
    if($param != '') array_push($clean_params_array, $param); 
}