Question

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.

Was it helpful?

Solution

I think str_replace() should work:

function remove_filter($url, $type, $value) {
    return str_replace("/$type/$value", "", $url);
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top