1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?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 !
sgrezzeria
February 7, 2008, February 07, 2008 20:46, permalink
Your code have some errors, but if I undestood right...
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$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
1 2 3 4 5 6 7 8 9
<? $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
1 2 3 4 5 6 7 8 9 10
<?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)
1 2 3 4 5 6 7 8 9 10 11 12 13
<?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();
Removing a list of parameters from an URL passed via a string. I don't even know if this code works, just guessing quickly.