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.

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 !

528de67899017dec3c7568b0ad0622ea

sgrezzeria

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

No rating. Login to rate!

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

Eineki

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

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

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

2 ratings. Login to rate!
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);
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)

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

Your refactoring





Format Copy from initial code

or Cancel