Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PHP: Removing specific parameters from querystring.

Posted on 2009-04-15
6
Medium Priority
?
827 Views
Last Modified: 2013-12-13
I want to create a set of links that remove specific parameters from a querysting.

For example
If Querystring = ?A=1&B=2&C=3&D=4
I want a link to Remove "A" and return ?B=2&C=3&D=4.
I then also want some links to remove "A" and "B" and return ?C=3&D=4.
Additionally, i then may need a link to remove "A" and "B" and add "E=5"

I'm not sure of the best way to do this.
Currently i've done this by using str_replace() for each parameter i want a link to remove, however, doing this for all the possible parameters is resulting in over 150 lines of code.

It's occured to me to use explode() to create a master array of the querysting and then creating a new array with the specifically removed parameter(S) using implode("&",) function.
 
However, i'm not having much luck.
0
Comment
Question by:ticallian
  • 3
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
kurijov earned 2000 total points
ID: 24154975
In the snippet you can see the class and example of use.
Have a nice day))

<?
class uri {
	public  $request_array = array();
	
	public function add($name, $value) {
		$this->request_array[$name] = $value;
	}
	
	public function remove($name) {
		unset($this->request_array[$name]);
	}
	
	public function setRequest($request) {
		$request_begin = strpos($request, '?');
		if ($request_begin !== false) {
			$params = explode('&', substr($request, $request_begin + 1));
			foreach ($params as $parameter) {
				$name_value = explode('=', $parameter);
				$this->add($name_value[0], $name_value[1]);
			}
		}
	}
	
	public function toString() {
		$string = "";
		foreach ($this->request_array as $key => $value) {
			$string = $string.'&'.$key.'='.$value;
		}
		return $string;
	}
}
//::::example
$parser = new uri();
$parser->setRequest('?A=1&B=2&C=3&D=4');
$parser->remove('B');
$parser->add('E', 7);
echo $parser->toString();
?>

Open in new window

0
 
LVL 48

Expert Comment

by:hernst42
ID: 24155019
See http://www.php.net/parse_str to convert the string to a php-array and http://www.php.net/manual/en/function.http-build-query.php to make the array to a string
0
 

Author Comment

by:ticallian
ID: 24155150
Thanks kurijov,

The example works fine as you've done it but i'm unsure on how to manipulate it exactly to my problem.

Rather than using a set string of "?A=1&B=2&C=3&D=4" i need it to read from a variable i have (i.e. $MyURL = $_SERVER['QUERY_STRING'])


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Expert Comment

by:kurijov
ID: 24155157
Pass $_SERVER['QUERY_STRING'] to setRequest method and all be fine))
0
 

Author Comment

by:ticallian
ID: 24155233
Thanks again kurijov for all the help,

$_SERVER['QUERY_STRING'] doesn't return the '?' required at the beginning of the querystring for your setRequest function to work.

To get around that, i wanted to create the $MyURL variable with an appended '?' to use within the setRequest. Is this still possible to use my own variables?
Alternatively i'm now using "$parser->setRequest('?'.$_SERVER['QUERY_STRING']);" which works.

However, i'm left with two small problems...
1. I have to manually append the '?' back onto the echo string "echo '?'.$parser->toString();" - Can this be done by the toString() function?
2. The new querystrings all begin with "&" ie "?&A=1" rather than "?A=1" - Where can i apply something alike to ltrim('&', $x) to get rid of it?
0
 

Author Comment

by:ticallian
ID: 24155938
I've tweaked the original code to fix the small problems mentioned above.

To anyone that has a similar problem, see my amended code below.
<?
class url {
        public  $request_array = array();
        
        public function add($name, $value) {
                $this->request_array[$name] = $value;
        }
        
        public function remove($name) {
                unset($this->request_array[$name]);
        }
        
        public function setRequest($request) {
                $request_begin = strpos($request, '?');
                if ($request_begin !== false) {
                        $params = explode('&', substr($request, $request_begin + 1));
                        foreach ($params as $parameter) {
                                $name_value = explode('=', $parameter);
                                $this->add($name_value[0], $name_value[1]);
                        }
                }
        }
 
		public function toString() {
                $string = "";
				$rowcount = 1;
                foreach ($this->request_array as $key => $value) {
                        IF ($rowcount == 1) { $string = $string.'?'.$key.'='.$value; $rowcount++; }
                        ELSE { $string = $string.'&'.$key.'='.$value; }
                }
                return $string;
        }
}
//::::example
$parser = new url();
$parser->setRequest('?'.$_SERVER['QUERY_STRING']);
$parser->remove('x');
$parser->add('y',7);
 
echo $parser->toString(); 
?>

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question