<?php
$params = array('SID','t','p','hilite');
$ref = 'url.php?t=4&p=5&sid=ddf175fd02216cbe9fab9a4b528d7185&hilite=2c23tra#link';
$setup = explode('&',$ref);
foreach ($parameter in $setup) {
if (in_array($parameter,$params)) {
$setup['$parameter'] = '';
}
}
?>
Refactorings
No refactoring yet !
dabdu
February 7, 2008, February 07, 2008 20:46, permalink
Your code have some errors, but if I undestood right...
$ref = 'url.php?t=4&p=5&sid=ddf175fd02216cbe9fab9a4b528d7185&hilite=2c23tra#link';
$params = array('sid','t','p','hilite');
function cleanQueryString($ref, $params = NULL){
$newparams = array();
preg_match_all('|([^?&#=]+)=([^?&#=]+)(#{0,}[^?&#=]*)|', $ref, $out, PREG_SET_ORDER);
foreach($out as $elem)
if(!in_array($elem[1], $params))
$newparams[] = "{$elem[1]}={$elem[2]}";
return implode('&',$newparams) . $out[count($out)-1][3];
}
echo cleanQueryString($ref, $params);
Eineki
February 7, 2008, February 07, 2008 23:53, permalink
<?
$params = array('SID','t','p','hilite');
$ref = 'url.php?t=4&p=5&sid=ddf175fd02216cbe9fab9a4b528d7185&hilite=2c23tra#link';
$urlParams = parse_str(parseurl($ref, PHP_URL_QUERY););
$survivingParams = array_diff_key($urlsParams, array_fill_keys($params, 0));
?>
Martindale
February 8, 2008, February 08, 2008 03:35, permalink
I need it to return the original domain, too - any ideas on how to do that?
hubfactor
February 8, 2008, February 08, 2008 11:52, permalink
<?php
$url = 'http://www.example.com/url.php?t=4&p=5&sid=ddf175fd02216cbe9fab9a4b528d7185&hilite=2c23tra#link';
$remove = array('sid','t');
$parsed = parse_url($url);
parse_str($parsed['query'], $params);
$parsed['params'] = array_diff_key($params, array_fill_keys($remove, 0));
print_r($parsed);
Eineki
February 8, 2008, February 08, 2008 12:09, permalink
If you call parse_url without the second argument it returns an hash array with keys scheme, host, user, pass, path, query, fragment.
You can use host, I guess.
Daniel O'Connor
March 20, 2008, March 20, 2008 12:08, permalink
Use pear's Net_URL (pear install Net_URL)
<?php require_once 'Net/URL.php'; $text = 'http://www.example.com/url.php?t=4&p=5&sid=ddf175fd02216cbe9fab9a4b528d7185&hilite=2c23tra#link'; $url = new Net_URL($text); print_r($url->querystring); unset($url->querystring["t"]); print_r($url); print $url->getURL();
Justin
July 28, 2009, July 28, 2009 18:21, permalink
If you're trying to use this as a link, you could just do this.
Notice the LACK of a space between the question mark and the equals sign.
<a href="<?=$_SERVER["PHP_SELF"]?>"
Gilmar Pupo
July 19, 2011, July 19, 2011 11:20, permalink
Try this:
$url = parse_url($_SERVER['REQUEST_URI']);
parse_str($url['query'], $params);
$url = $url['path'] . '?' .
http_build_query(
array_diff_key(
$params,
array_fill_keys(array('SID', 'sid'), 0)
)
);
//Send Moved Permanently header
header("HTTP/1.1 301 Moved Permanently");
//Redirect to clean URL
header("Location: " . trim($url));
exit();
Gilmar Pupo
July 19, 2011, July 19, 2011 11:20, permalink
Try this:
$url = parse_url($_SERVER['REQUEST_URI']);
parse_str($url['query'], $params);
$url = $url['path'] . '?' .
http_build_query(
array_diff_key(
$params,
array_fill_keys(array('SID', 'sid'), 0)
)
);
//Send Moved Permanently header
header("HTTP/1.1 301 Moved Permanently");
//Redirect to clean URL
header("Location: " . trim($url));
exit();
Removing a list of parameters from an URL passed via a string. I don't even know if this code works, just guessing quickly.