0

I have a url that in the worst case scenario looks like this website.com/clothes/women/type/tshirts/brand/nike/color/red/size/s/price/0-29

This is created by clicking on links that will filter the results starting from website.com/clothes/women. The problem is how can I reverse it ? I show all the filters clicked with a X near it, so when clicked, a certain filter will be deleted and the page refreshes. If I delete the color filter, /color/red or color/red/ must be deleted from the url.

How can this be done ? I tried with str_replace() but with no luck.

Just an input and I will find the way.

Thank you.

10
  • @meagar: what is the difference actually? Why is it worse than ?type=tshirts&...? Commented May 8, 2014 at 0:22
  • @meagar I don't know what you mean, this is a query string, it's just formatted with .htaccess so it does not look like ?type=tshirts&brand=nike&... Can you suggest a better one ? Commented May 8, 2014 at 0:24
  • @C.Ovidiu use POST and session data with cookies like every other site on the planet. Commented May 8, 2014 at 0:25
  • @Deryck: it's actually the terrible advice (not to say it's silly). Check amazon and ebay Commented May 8, 2014 at 0:25
  • 1
    @zerkms I didn't look closely enough; I thought it was just an arbitrary series of key1/value1/key2/value2/key3/value3... but you're right, it specifically looks like it gets progressively more specific. Commented May 8, 2014 at 0:33

2 Answers 2

3

I think str_replace() should work:

function remove_filter($url, $type, $value) {
    return str_replace("/$type/$value", "", $url);
}
Sign up to request clarification or add additional context in comments.

Comments

2

This is not a job for str_replace or regexes.

Use explode to put the components of the path into an array. Manipulate the array. Reassemble the path with join.

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.