0

How can I remove ?cat= from the example 1 so it can look like example 2 using PHP.

Example 1

?cat=some-cat

Example 2

some-cat
11
  • 2
    Remove how and from what? Please add some more info about this (maybe some real world examples as well), there are parsing functions for this Commented Dec 26, 2010 at 21:02
  • @Pekka, I think the question says it all remove ?cat= ?cat=some-cat, ?cat=some-cat2, ?cat=some-cat3 and so on. Commented Dec 26, 2010 at 21:06
  • @common no, not necessarily. You are showing a query string. Can it contain more parameters than ?cat=? If no, a simple str_replace() will be sufficient. If yes, you will need to properly parse the string Commented Dec 26, 2010 at 21:10
  • 1
    @common: If that is just the page querystring you just need to look at $_GET... you should probably clarify the question Commented Dec 26, 2010 at 21:17
  • 1
    I give up - you're not giving enough information to answer this in depth. If str_replace() works for you, the problem is solved; otherwise, you will need to explain what you are doing. Commented Dec 26, 2010 at 21:19

2 Answers 2

2

Easy, use str_replace:

$cat = '?cat=some-cat';
$cat = str_replace('?cat=', '', $cat);

EDIT:

If you are pulling this query string through something like $_SERVER['QUERY_STRING'], then I'd opt for you to use $_GET, which is an associative array of the GET variables passed to your script, so if your query string looked like this:

?cat=some-cat&dog=some-dog

The $_GET array would look like this:

(
   'cat' => 'some-cat',
   'dog' => 'some-dog'
)


$cat = $_GET['cat']; //some-cat
$dog = $_GET['dog']; //some-dog

Another edit:

Say you had an associative array of query vars you wish to append onto a URL string, you'd do something like this:

$query_vars = array();
$query_vars['cat'] = 'some-cat';
$query_vars['dog'] = 'some-dog';

foreach($query_vars as $key => $value) {
   $query_vars[] = $key . '=' . $value;
   unset($query_vars[$key]);
}

$query_string = '?' . implode('&', $query_vars); //?cat=some-cat&dog=some-dog
Sign up to request clarification or add additional context in comments.

2 Comments

what if I dont now how many variables are going top be passed?
@common You can build up an associative array and then iterate through it.
1

Yes, you can, very easily:

$cat = '?cat=some-cat';
$cat = substr($cat, 5); // remove the first 5 characters of $cat

This may not be the best way to do this. That will depend on what you are attempting to achieve...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.