• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

Removing bits and pieces from a url

i have a script that tells a page to display things depending on whats in a value thats gotten from the location of the page

the location looks something like
http://mysite/blah?blah=id,id,id,id

i want to add links so people can select which ids they want to view and which ones they dont but im not sure how to safely modify the location

how could i remove something say in the middle of that?
0
tsmitt
Asked:
tsmitt
  • 2
  • 2
1 Solution
 
SynstheCommented:
That's not too hard to do with PHP, using arrays and conditional statements. =) Try something like the following, you can use it directly or make a function out of it for repeated use.

$value = explode(",", $value);
for ($i = 0; $i < count($value); $i++) {
    $bits = explode(".", $value[$i]);

    if ($bits[0] != $remove) {
        $newvalue .= $value[$i] . ",";
    }
}

$newvalue = preg_replace("/,$/", "", $newvalue);

Since php puts everything into variables by default, $value would actually be $blah in accordance with your example above. What this is doing, is splitting the value by the ,'s and looping through to see if it's the one you don't want (designated by $remove, edit that as you want), and putting the rest in a new comma delimited value.

HTH
0
 
tsmittAuthor Commented:
hmm whats the point of the $bits variable?
0
 
SynstheCommented:
Sorry, I cut and pasted the code piece from one of my scripts where I do something very similar to what I believe you're describing. I merely forgot to remove that part. You could do:

1: $value = explode(",", $value);
2: for ($i = 0; $i < count($value); $i++) {
3:        if (!preg_match("/$remove/", $value[$i])) {
4:            $newvalue .= $value[$i] . ",";
5:        }
6: }
7:
8: $newvalue = preg_replace("/,$/", "", $newvalue);

Which probably makes more sense for you. Line 1 is where you split the value from the url into an array, seperated by , or whatever else you may want to use. The second line starts a loop to go through each value in the array that was created, and 3-5 checks to make sure it's not a value you want to remove before creating the new value. Finally, line 8 removes any trailing commas you may have to clean up the look.
0
 
tsmittAuthor Commented:
great that worked! thanx
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now