PHP Function: Remove a Query String Key=>Value

In: web resources

18 Jan 2010

I was recently coding a website which relied heavily on query string variables. It was absolutely imperative that I keep the query string clean and not duplicate keys. I needed to find a PHP function that would allow me to remove keys (and their values) easily and reliably. Enter a PHP function I found at Added Bytes:

/* the function */
function remove_querystring_var($url, $key) {
	$url = preg_replace('/(.*)(?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
	$url = substr($url, 0, -1);
	return $url;
}

/* usage */
//pretending this page is

All I needed to do was provide the URL (in my case I wanted the current page’s URI) and the key to remove — so simple!

All credit goes to Added Bytes for their awesome work. It saved me a lot of time…hopefully it does for you too!

Don’t forget to follow me on Twitter and be sure to visit Script & Style for the best Javascript and CSS articles around!

Sponsor the David Walsh Blog and get your brand in front of several thousand users per day!

PHP Function: Remove a Query String Key=>Value

Related posts:

  1. Change the PHP Query String Variable Separator Using php.ini
  2. PHP Function To Get A File Extension From A String
  3. Generate Readable Byte Labels Using PHP
  4. PHP, Microsoft SQL Server (MSSQL), and IIS: Connect and Query with ODBC
  5. Remove Broken Images Using MooTools or jQuery


Go to Source

4 Responses to PHP Function: Remove a Query String Key=>Value

Avatar

who cares

January 29th, 2010 at 8:11 am

No BS please.

Avatar

Joe

March 17th, 2010 at 4:10 am

okey you got a few things mixed up there.

so, php is used to generate dynamic content, not to design the site.

IFrames are bad and evil. If u use them too much they'll take over the internet and kill us all. Do not use them.

div's your friend. use them for a general structure and then work with css to style them. Divs are friendly.

btw: Tables are also evil. Only use them to display data that needs to be in a table.

Avatar

Lance #1 Davis

March 19th, 2010 at 7:14 am

Trial and error:
|2x-1|>3
|2(5)-1|>3 = |10-1|>3 = |9|>3 =9>3 = True
|2(3)-1|>3 = |6-1|>3 = |5|>3 =5>3 = True
|2(-1)-1|>3 = |-2-1|>3 = |-3|>3 =3>3 = False
|2(-4)-1|>3 = |7-1|>3 = |6|>3 =6>3 = True
|2(10)-1|>3 = |20-1|>3 = |19|>3 =19>3 = True

Thus C (-1) is your answer

Avatar

Rafe

March 24th, 2010 at 1:36 pm

it probably comes (somehow) from an html email and dropped the actual code that makes it work.

& nbsp; in html is a non breaking space. In other words a normal space.

It is usually used when someone wants more than one space between words/things as a web browser (or email client viewing html email) will condense more than one empty space into just one of them.

Comment Form

About this blog

This blog delivers stylish and dynamic news for designers and web-developers on all subjects of design, ranging from: CSS, Ajax, Javascript, web design, graphics, typography, advertising & much more. Our goal is to help you communicate effectively on the web with an engaging website or functional interface.