Reputation: 1134
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.
Upvotes: 0
Views: 120
Reputation: 781721
I think str_replace()
should work:
function remove_filter($url, $type, $value) {
return str_replace("/$type/$value", "", $url);
}
Upvotes: 3
Reputation: 93725
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
.
Upvotes: 2