最好的方法是使用parse_url作为 HTTP_REFERER,然后使用 parse_str作为查询字符串,当然最好进行某种验证,以便处理错误。
更新示例 // 这是一个更复杂的示例,但可以让您完全控制。
$previousurlfull = 'http://stackoverflow.com/?view=listcats&sup=1&test=true';
$clean_link = parse_url($previousurlfull);
parse_str($clean_link['query'], $args);
// validation need
$link = $clean_link['scheme'].'://'.$clean_link['host'].$clean_link['path'];
$link .= '?'; // add ? to the beggining
// use Iterator so you can use hasNext
$iterator = new CachingIterator(new ArrayIterator($args));
foreach($iterator as $k => $v){
// if doesnt have the view query key
if($k != 'view'){
$link .= $k.'='.$v; // insert the key and value
if($iterator->hasNext()){
$link .= '&'; // if it has another item on the array insert &
}
}
}
echo $link;
// output = http://stackoverflow.com/?sup=1&test=true
工作示例:示例
旧示例;
$previousurlfull = 'http://stackoverflow.com/?view=listcats&sup=1&test=true';
$clean_link = parse_url($previousurlfull); // Parse URL
parse_str($clean_link['query'], $args); // Parse Query String to an array
echo '<pre>'; // clean output
var_dump($clean_link);
echo $clean_link['scheme'].'://'.$clean_link['host'].$clean_link['path'];
var_dump($args);
echo $args['test'];
输出
array(4) {
["scheme"]=>
string(4) "http"
["host"]=>
string(17) "stackoverflow.com"
["path"]=>
string(1) "/"
["query"]=>
string(29) "view=listcats&sup=1&test=true"
}
echo $clean_link['scheme'].'://'.$clean_link['host'].$clean_link['path']; // example
// $args Query Output
array(3) {
["view"]=>
string(8) "listcats"
["sup"]=>
string(1) "1"
["test"]=>
string(4) "true"
}
工作示例:示例