72f4ba51b784673a15a1e89d8d9f49d1

Removing a list of parameters from an URL passed via a string. I don't even know if this code works, just guessing quickly.

<?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 !

528de67899017dec3c7568b0ad0622ea

dabdu

February 7, 2008, February 07, 2008 20:46, permalink

No rating. Login to rate!

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);
5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

February 7, 2008, February 07, 2008 23:53, permalink

No rating. Login to rate!
<?
$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));

?>
72f4ba51b784673a15a1e89d8d9f49d1

Martindale

February 8, 2008, February 08, 2008 03:35, permalink

No rating. Login to rate!

I need it to return the original domain, too - any ideas on how to do that?

Ed9c50a6db8b5e078b5ef84306a8477c

hubfactor

February 8, 2008, February 08, 2008 11:52, permalink

3 ratings. Login to rate!
<?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);
5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

February 8, 2008, February 08, 2008 12:09, permalink

No rating. Login to rate!

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.

353d83d3677b142520987e1936fd093c

Daniel O'Connor

March 20, 2008, March 20, 2008 12:08, permalink

No rating. Login to rate!

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();
D41d8cd98f00b204e9800998ecf8427e

Justin

July 28, 2009, July 28, 2009 18:21, permalink

No rating. Login to rate!

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"]?>"
Ee7e4705dd4ac06adfe650c2cdc39bdd

jones

June 3, 2011, June 03, 2011 19:43, permalink

No rating. Login to rate!
Eq7L9j http://fJiI3nP9cXwPpdjgsN02bu.com
Ee7e4705dd4ac06adfe650c2cdc39bdd

jones

June 3, 2011, June 03, 2011 19:43, permalink

No rating. Login to rate!
Eq7L9j http://fJiI3nP9cXwPpdjgsN02bu.com
Ee7e4705dd4ac06adfe650c2cdc39bdd

jones

June 3, 2011, June 03, 2011 19:44, permalink

No rating. Login to rate!
Eq7L9j http://fJiI3nP9cXwPpdjgsN02bu.com
Ee7e4705dd4ac06adfe650c2cdc39bdd

jones

June 3, 2011, June 03, 2011 19:44, permalink

No rating. Login to rate!
Eq7L9j http://fJiI3nP9cXwPpdjgsN02bu.com
A43cc5410efe38d65a66086940e3482c

Gilmar Pupo

July 19, 2011, July 19, 2011 11:20, permalink

No rating. Login to rate!

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();
A43cc5410efe38d65a66086940e3482c

Gilmar Pupo

July 19, 2011, July 19, 2011 11:20, permalink

No rating. Login to rate!

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();

Your refactoring





Format Copy from initial code

or Cancel